#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 998244353 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

ll power(int k, ll n, int M){
  if(n==0) return 1;
  if(n==1) return (ll)k;
  
  ll ret = power(k,n/2,M);
  
  ret=(ret*ret)%M;
  
  if(n%2==1)
    ret=(ret*k)%M;
  
  return ret;
}

map<pair<int,int>,int> mm;

ll dp[SIZE][512];

int main(){
  int n, m;

  scanf("%d%d", &n, &m);

  ll ans2 = 1, counter = ((ll)n*n - (n + 2 * (n-1) + 2 * (n-2))) / 2;
  
  for(int i=0;i<m;i++){
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--; b--;
    mm[{a,b}] = c;

    if(abs(b - a) > 2) {
      if(mm.find({b,a}) == mm.end()){
        counter--;
      }else{
        if(mm.find({b,a})->second != c){
          ans2 = 0;
        }
      }
    }
  }

  debug(counter);
  
  ans2 = ans2 * power(2, counter, mod);

  if(n == 2){
    int ans = 0;
    for(int i=0;i<(1<<4);i++){
      if(__builtin_popcount(i) % 2) continue;

      if(mm.find({0,0}) != mm.end() && mm.find({0,0})->second != ((i>>0)&1)) continue;
      if(mm.find({0,1}) != mm.end() && mm.find({0,1})->second != ((i>>1)&1)) continue;
      if(mm.find({1,0}) != mm.end() && mm.find({1,0})->second != ((i>>2)&1)) continue;
      if(mm.find({1,1}) != mm.end() && mm.find({1,1})->second != ((i>>3)&1)) continue;
      ans++;
    }

    cout << ans << endl;
    
    return 0;
  }

  vector<int> vec, vecs[16];

  
  for(int i=0;i<(1<<9);i++){
    int a[3][3];
    for(int j=0;j<9;j++)
      a[j/3][j%3] = (i>>j)&1;
    
    if(__builtin_popcount(i)%2) continue;

    if((a[0][0] + a[1][0] + a[1][1] + a[0][1])%2) continue;
    if((a[1][1] + a[2][1] + a[2][2] + a[1][2])%2) continue;

    vec.push_back(i);
    vecs[a[0][0] * 8 + a[0][1] * 4 + a[1][0] * 2 + a[1][1]].push_back(i);
  }
  
  ll ans = 0;
  
  for(int o=0;o<vec.size();o++){
    int start = vec[o];

    dp[1][start] = 1;
  }

  int a[3][3];
  for(int i=1;i<n-1;i++){
    for(int j=0;j<vec.size();j++){
      int mat = vec[j];
      
      bool f = false;
      for(int j=0;j<9;j++){
        a[j/3][j%3] = (mat>>j)&1;
        auto it = mm.find({j/3 + i - 1, j%3 + i - 1});
        // if(it != mm.end()) { debug(j/3 + i - 1); debug(j%3 + i - 1); }
        if(it != mm.end() && it->second != a[j/3][j%3]) f = true;
      }
      
      if(f) { dp[i][mat] = 0; continue; }
      
      //debug(dp[i][mat]);
      
      if(i == n-2) {
        ans = (ans + dp[i][mat]) % mod;
      } else {
        int nextlu = a[1][1] * 8 + a[1][2] * 4 + a[2][1] * 2 + a[2][2];
        for(int k=0;k<vecs[nextlu].size();k++){
          dp[i+1][vecs[nextlu][k]] += dp[i][mat];
          dp[i+1][vecs[nextlu][k]] %= mod;
        }
      }
    }
  }

  debug(ans2);
  cout << ans * ans2 % mod << endl;

  
  return 0;
}




Measured memory_bytes: 404864