#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

const long MOD = 998244353;
int n, m;
map<P, bool> mp;

long DP[100000][16];

bool valid(int i, int s) {
  for (int x = 0; x < 3; ++x) {
    for (int y = 0; y < 3; ++y) {
      P p(i + x, i + y);
      if (!mp.count(p)) continue;
      if (mp[p] != ((s >> (x * 3 + y)) & 1)) return false;
    } 
  }
  return true;
}

long dog() {
  if (n == 2) {
    if (m == 4) {
      int sum = 0;
      for (auto&& p : mp) {
        sum += p.second;
      }
      return !(sum % 2);
    }
    return 1 << (3 - m);
  }
  
  for (int s = 0; s < (1 << 4); ++s) {
    DP[0][s] = __builtin_popcount(s) % 2 == 0;
  }
  
  for (int i = 0; i < n - 2; ++i) {
    int mask = 0;
    int val = 0;
    for (int x = 0; x < 3; ++x) {
      for (int y = 0; y < 3; ++y) {
        P p(i + x, i + y);
        int k = x * 3 + y;
        if (mp.count(p)) {
          mask |= 1 << k;
          val |= mp[p] << k;
        }
      } 
    }
    
    for (int s = 0; s < (1 << 9); ++s) {
      if ((s & mask) != val) continue;
      if (__builtin_popcount(s) % 2 != 0) continue;
      if (__builtin_popcount(s & 0b000011011) % 2 != 0) continue;
      if (__builtin_popcount(s & 0b110110000) % 2 != 0) continue;
      
      int t = ((s & 0b000011000) >> 1) | (s & 0b000000011);
      int u = ((s & 0b110000000) >> 5) | ((s & 0b000110000) >> 4);
      DP[i + 1][u] += DP[i][t];
      DP[i + 1][u] %= MOD;
    }
  }
  
  long sum = 0;
  for (int s = 0; s < (1 << 4); ++s) {
    sum += DP[n - 2][s];
    sum %= MOD;
  }
  return sum;
}

long mpow(long n) {
  if (n == 0) return 1;
  if (n % 2 == 1) return mpow(n - 1) * 2 % MOD;
  long tmp = mpow(n / 2);
  return tmp * tmp % MOD;
}

long cat() {
  set<P> st;
  for (auto&& q : mp) {
    P a = q.first;
    if (abs(a.second - a.first) < 3) continue;
    P b(a.second, a.first);
    if (mp.count(b) && mp[a] != mp[b]) return 0;
    st.insert(a);
    st.insert(b);
  }
  
  return mpow((long(n - 2) * (n - 3) - st.size()) / 2);
}

long sub() {
  cin >> n >> m;
  for (int j = 0; j < m; ++j) {
    int a, b, c;
    cin >> a >> b >> c;
    --a; --b;
    mp[P(a, b)] = c;
  }
  return cat() * dog() % MOD;
}

int main() {
  cout << sub() << endl;
}