#include <bits/stdc++.h>
      
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); i++)
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())

#define pb emplace_back
#define fi first
#define se second

using namespace std;

using ld = long double;
using ll = long long int;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ll MOD = 998244353;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> void UNIQUE(vector<T> &a){ a.erase(unique(a.begin(), a.end()), a.end()); }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

ll in(){long long int x; scanf("%lld", &x); return x;}
ld fin(){double x; scanf("%lf", &x); return x;}
char yuyushiki[1000010]; string stin(){scanf( "%s", yuyushiki ); return yuyushiki;}

// head

struct Mod{
  unsigned n;
  Mod() : n(0){}
  Mod( ll x ){
    if( x < 0 ) n = x%MOD+MOD;
    else n = x%MOD;
  }
};
Mod operator + ( Mod a  , Mod b ){ return Mod( a.n + b.n ); }
Mod operator +=( Mod &a , Mod b ){ return a = a + b; }
Mod operator - ( Mod a ){ return Mod( MOD - a.n ); }
Mod operator - ( Mod a  , Mod b ){ return Mod( a.n + MOD - b.n ); }
Mod operator -=( Mod &a , Mod b ){ return a = a - b; }
Mod operator * ( Mod a  , Mod b ){ return Mod( (ll)a.n * b.n ); }
Mod operator *=( Mod &a , Mod b ){ return a = a * b; }
Mod modpow( Mod x , ll k ){
  Mod res = 1;
  while( k ){
    if( k & 1 ) res *= x;
    k /= 2;
    x *= x;
  }
  return res;
}
ll extgcd( ll a , ll b , ll &x , ll &y ){
  ll d = a;
  if( b != 0 ){
    d = extgcd( b , a % b , y , x );
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
  return d;
}
Mod inv( Mod a ){ ll x, y; assert( extgcd( a.n , MOD , x , y ) == 1 ); return Mod( x ); }
Mod operator / ( Mod a  , Mod b ){ return Mod( (ll)a.n * inv(b).n ); }
Mod operator /=( Mod &a , Mod b ){ return a = a / b; }

using pi = pair<int, int>;

Mod dp[100010][2];

int main(){

  int n = in();
  int m = in();
  map<pi, int> ma;
  REP(i, m){
    int a = in() - 1;
    int b = in() - 1;
    int c = in();
    ma[pi(a, b)] = c;
  }

  auto get = [&](ll y, ll x){
    if(ma.find(pi(y, x)) == ma.end()){
      return -1;
    }
    return ma[pi(y, x)];
  };

  Mod rat = 1;
  
  ll cnt = 0;
  YYS(w, ma){
    int y = w.fi.fi;
    int x = w.fi.se;
    if(abs(x - y) >= 3){
      if(get(x, y) == -1){
        cnt++;
      } else if(get(x, y) != w.se){
        rat = 0;
      }
      cnt++;
    }
  }

  assert(((ll)n * n - 5 * (n - 2) - 4 - cnt) % 2 == 0);
  rat *= modpow(2, ((ll)n * n - 5 * (n - 2) - 4 - cnt) / 2);

  // cout << rat.n << endl;
  
  if(get(0, 0) == -1){
    dp[0][0] = 1;
    dp[0][1] = 1;
  } else {
    dp[0][get(0, 0)] = 1;
  }

  REP(i, n-1){
    REP(a, 2){
      if(i > 0){
        ll e = get(i-1, i+1);
        ll f = get(i+1, i-1);
        if(e == -1 && f == -1){
          dp[i][a] *= 2;
        } else if(e != -1 && f != -1 && (a + e + f) % 2 != 0){
          dp[i][a] = 0;
        }
      }
      int cb = get(i, i+1);
      int cc = get(i+1, i);
      int cd = get(i+1, i+1);
      REP(b, 2){
        if(cb != -1 && cb != b){
          continue;
        }
        REP(c, 2){
          if(cc != -1 && cc != c){
            continue;
          }
          REP(d, 2){
            if(cd != -1 && cd != d){
              continue;
            }
            if((a + b + c + d) % 2 == 0){
              dp[i+1][d] += dp[i][a];
            }
          }
        }
      }
    }
  }

  Mod ans = (dp[n-1][0] + dp[n-1][1]) * rat;

  printf("%lld\n", (ll)ans.n);
  
  return 0;
}


Measured memory_bytes: 4096