#include<iostream>
#include<algorithm>
#include<map>
#include<set>

using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;

map<P, Int> dict;
#define MOD  998244353LL;
Int a, b, c;
void ng(){
  cout << 0 << endl;
  exit(0);
}
Int pw(Int a, Int b){
  if(b == 0)return 1;
  Int res = pw(a, b/2);
  res *= res;
  res %= MOD;
  if(b % 2 == 1)res *= a;
  return res % MOD;
}
Int u[108000];
Int r(Int x){
  if(u[x] == x)return x;
  return u[x] = r(u[x]);
}
void unite(Int x, Int y){
  x = r(x);
  y = r(y);
  if(x != y)u[x] = y;
}
Int n, m, cnt;
int main(){
  for(Int i = 0;i < 108000;i++)u[i] = i;
  cin >> n >> m;
  cnt = (n * n + n) / 2;
  set<P> decided;
  for(Int i = 0;i < m;i++){
    cin >> a >>b >> c;
    dict[P(a,b)] = c;
    decided.insert(P(a,b));
  }
  for(auto p: decided){
    Int a = p.first;
    Int b = p.second;
    Int c = dict[p];
    if(abs(a-b) > 2){
      if(dict.count(P(b,a))){
	if(dict[P(b,a)] != c)ng();
      }
      dict[P(b,a)] = c;
    }
    if(abs(a-b) == 2){
      Int mid = (a+b) / 2;
      if(dict.count(P(mid, mid)) && dict.count(P(b, a))){
	if((dict[P(a,b)] + dict[P(mid, mid)] + dict[P(b, a)]) % 2 == 1)ng();
      }
      else if(dict.count(P(b, a))){
	dict[P(mid, mid)] = (dict[P(a,b)] + dict[P(b,a)]) % 2;
      }
      else if(dict.count(P(mid, mid))){
	dict[P(b, a)] = (dict[P(a,b)] + dict[P(mid,mid)]) % 2;
      }
      else{
	if(a > b){
	  dict[P(b,a)] = dict[P(a,b)];
	  dict.erase(P(a,b));
	}
      }
    }
  }
  for(Int i = 1;i < n;i++){
    Int hoge = 0, fuga = 0;
    if(dict.count(P(i,i)))hoge++,fuga+=dict[P(i,i)];
    if(dict.count(P(i,i+1)))hoge++,fuga+=dict[P(i,i+1)];
    if(dict.count(P(i+1,i)))hoge++,fuga+=dict[P(i+1,i)];
    if(dict.count(P(i+1,i+1)))hoge++,fuga+=dict[P(i+1,i+1)];
    if(hoge == 4)if(fuga % 2)ng();
    if(hoge == 3){
      if(dict.count(P(i,i)) == 0)dict[P(i,i)] = fuga % 2;
      if(dict.count(P(i,i+1)) == 0)dict[P(i,i+1)] = fuga % 2;
      if(dict.count(P(i+1,i)) == 0)dict[P(i+1,i)] = fuga % 2;
      if(dict.count(P(i+1,i+1)) == 0)dict[P(i+1,i+1)] = fuga % 2;
    }
    if(hoge <= 2 && dict.count(P(i+1, i)) && dict.count(P(i,i+1)) == 0){
      dict[P(i,i+1)] = dict[P(i+1,i)];
      dict.erase(P(i+1,i));
    }
  }
  for(Int i = n-1;i >= 1;i--){
    Int hoge = 0, fuga = 0;
    if(dict.count(P(i,i)))hoge++,fuga+=dict[P(i,i)];
    if(dict.count(P(i,i+1)))hoge++,fuga+=dict[P(i,i+1)];
    if(dict.count(P(i+1,i)))hoge++,fuga+=dict[P(i+1,i)];
    if(dict.count(P(i+1,i+1)))hoge++,fuga+=dict[P(i+1,i+1)];
    if(hoge == 4)if(fuga % 2)ng();
    if(hoge == 3){
      if(dict.count(P(i,i)) == 0)dict[P(i,i)] = fuga % 2;
      if(dict.count(P(i,i+1)) == 0)dict[P(i,i+1)] = fuga % 2;
      if(dict.count(P(i+1,i)) == 0)dict[P(i+1,i)] = fuga % 2;
      if(dict.count(P(i+1,i+1)) == 0)dict[P(i+1,i+1)] = fuga % 2;
    }
  }

  for(Int i = 1;i < n;i++){
    Int hoge = 0, fuga = 0;
    if(dict.count(P(i,i)))hoge++,fuga+=dict[P(i,i)];
    if(dict.count(P(i,i+1)))hoge++,fuga+=dict[P(i,i+1)];
    if(dict.count(P(i+1,i)))hoge++,fuga+=dict[P(i+1,i)];
    if(dict.count(P(i+1,i+1)))hoge++,fuga+=dict[P(i+1,i+1)];
    if(hoge == 2 && dict.count(P(i,i+1)) && dict.count(P(i+1,i))){
      unite(i, i+1);
    }
  }
  for(Int i = 1;i <= n;i++){
    if(dict.count(P(i,i)))cnt--;
    else if(r(i) != i)cnt--;
  }
  for(auto p: dict){
    Int a = p.first.first;
    Int b = p.first.second;
    //    cout << a << " " << b << " " << p.second << endl;
    if(a < b)cnt--;
  }
  cout << pw(2, cnt) << endl;
  return 0;
}

Measured memory_bytes: 10496