#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 998244353;
ll mpow(ll a, ll b, ll p = -1){
  ll ret = 1, num = a;
  if(p==-1){
    while(b>0){
      if(b%2) ret *= num;
      num = num * num;
      b /= 2;
    }
  }else{
    while(b>0){
      if(b%2) ret = (ret*num)%p;
      num = (num*num)%p;
      b /= 2;
    }
  }
  return ret;
}
void err(ll message=-1){
  std::cout << 0 << '\n';
  exit(0);
}
int main(int argc, char const *argv[]) {
  ll n, m;std::cin >> n >> m;
  map<ll, ll> mp;
  map<ll, ll> mid, one, two;
  set<ll> T;
  if(n==2){

  }
  for(int i=0;i<m;i++){
    ll a, b, c;scanf("%lld %lld %lld", &a, &b, &c);
    a--, b--;
    if(a==b){
      auto itr = mid.find(a);
      if(itr!=mid.end()){
        if((*itr).second!=c) err();
      }else{
        mid.emplace(a, c);
      }
    }else if(abs(a-b)==1){
      if(a>b) swap(a, b);
      auto itr = one.find(a*1000000+b);
      if(itr!=one.end()){
        ll tmp = c^(*itr).second;
        one.emplace(-1000000-(a*1000000+b), tmp);
      }else{
        one.emplace(a*1000000+b, c);
      }

    }else if(abs(a-b)==2){
      if(a>b) swap(a, b);
      T.insert(a*1000000+b);
      auto itr = two.find(a*1000000+b);
      if(itr!=two.end()){
        ll tmp = c^(*itr).second;
        auto center = mid.find(b-1);
        if(center==mid.end()) mid.emplace(b-1, tmp);
        else if((*center).second!=tmp) err();
        two.emplace(-1000000-(a*1000000+b), tmp);
      }else{
        two.emplace(a*1000000+b, c);
      }
    }else{
      if(a>b) swap(a, b);
      auto itr = mp.find(a*1000000+b);

      if(itr!=mp.end()){
        if(c!=(*itr).second) err(1);
      }else{
        mp.emplace(a*1000000 + b, c);
      }
    }
  }
  vvl dp = VV(n, 2, 0, ll);
  if(mid.find(0)==mid.end()) dp[0][0] = dp[0][1] = 1;
  else dp[0][mid.at(0)] = 1;

  for(ll i=1;i<n;i++){
    auto itr = mid.find(i);
    if(itr==mid.end()){
      auto t = two.find(-1000000-((i-1)*1000000+(i+1)));
      if(t==two.end()||(*t).second==1){
        auto o = one.find(-1000000-((i-1)*1000000+i));
        auto k = one.find((i-1)*1000000+i);
        if(o!=one.end()){
          dp[i][1] = dp[i-1][1^(*o).second];
        }else{
          if(k==one.end()) dp[i][1] = (2*(dp[i-1][0]+dp[i-1][1]))%P;
          else dp[i][1] = (dp[i-1][0]+dp[i-1][1])%P;
        }
      }
      if(t==two.end()||(*t).second==0){
        auto o = one.find(-1000000-((i-1)*1000000+i));
        auto k = one.find((i-1)*1000000+i);
        if(o!=one.end()){
          dp[i][0] = dp[i-1][(*o).second];
        }else{
          if(k==one.end()) dp[i][0] = (2*(dp[i-1][0]+dp[i-1][1]))%P;
          else dp[i][0] = (dp[i-1][0]+dp[i-1][1])%P;
        }
      }

    }else if((*itr).second==1){
      auto t = two.find(-1000000-((i-1)*1000000+(i+1)));
      if(t!=two.end()&&(*t).second!=1) err();
      auto o = one.find(-1000000-((i-1)*1000000+i));
      auto k = one.find((i-1)*1000000+i);
      if(o!=one.end()){
        dp[i][1] = dp[i-1][1^(*o).second];
      }else{
        if(k==one.end()){
          dp[i][1] = (2*(dp[i-1][0]+dp[i-1][1]))%P;
        }else {
          dp[i][1] = (dp[i-1][0]+dp[i-1][1])%P;
        }
      }
    }else{
      auto t = two.find(-1000000-((i-1)*1000000+(i+1)));
      if(t!=two.end()&&(*t).second!=0) err();
      auto o = one.find(-1000000-((i-1)*1000000+i));
      auto k = one.find((i-1)*1000000+i);
      if(o!=one.end()){
        dp[i][0] = dp[i-1][(*o).second];
      }else{
        if(k==one.end()) dp[i][0] = (2*(dp[i-1][0]+dp[i-1][1]))%P;
        else dp[i][0] = (dp[i-1][0]+dp[i-1][1])%P;
      }
    }
  }
  //std::cout << "----" << '\n';
  //for(int i=0;i<n;i++) std::cout << dp[i][0] << " " << dp[i][1] << '\n';
  //自由枠
  //std::cout << mp.size() << '\n';
  ll ml = ((n-2)*(n-3))/2 - mp.size() + n-2-T.size();
  ll ans = (mpow(2, ml, P) * (dp[n-1][0]+dp[n-1][1])%P)%P;
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 11520