// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// #include<deque>
// #include<multiset>
#include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

const ll mod = 998244353;

ll n, m;

int mov(int s) {
  int ns =
    ((s >> 4) & 1) << 0 |
    ((s >> 5) & 1) << 1 |
    ((s >> 7) & 1) << 3 |
    ((s >> 8) & 1) << 4;
  return ns;
}

int movDp[1 << 9];

int used[112345][1 << 9];
ll dp[112345][1 << 9];

bool check(int s) {
  if((((s >> 0) & 1) ^ ((s >> 1) & 1) ^ ((s >> 3) & 1) ^ ((s >> 4) & 1)) == 1) return 0;
  if((((s >> 4) & 1) ^ ((s >> 5) & 1) ^ ((s >> 7) & 1) ^ ((s >> 8) & 1)) == 1) return 0;
  int c = 0;
  for(int i = 0; i < 9; i++) {
    c ^= ((s >> i) & 1);
  }
  if(c == 1) return 0;
  return 1;
}

bool checkDp[1 << 9];

#if 0

0 1 2
3 4 5
6 7 8

#endif

int uni(int s, int t) {
  return
    s |
    ((t >> 0) & 1) << 2 |
    ((t >> 1) & 1) << 5 |
    ((t >> 2) & 1) << 6 |
    ((t >> 3) & 1) << 7 |
    ((t >> 4) & 1) << 8;
}

int w[112345][5];
ll solve(int i, int s) {
  if(used[i][s]) return dp[i][s];
  used[i][s] = 1;

  if(!checkDp[s]) return dp[i][s] = 0;
  int cnt = 0;
  for(int j = -1; j <= 1; j++) for(int k = -1; k <= 1; k++) {
    int y = i + j;
    int x = i + k;
    if(w[y][x-(y-2)] && w[y][x-(y-2)] - 1 != ((s >> cnt) & 1)) {
      return dp[i][s] = 0;
    }
    cnt++;
  }
  if(i == n - 2) {
    return dp[i][s] = 1;
  }
  int ns = movDp[s];
  ll res = 0;
  for(int j = 0; j < 1 << 5; j++) {
    int nns = uni(ns, j);
    res += solve(i + 1, nns);
  }
  return dp[i][s] = res % mod;
}

int f[5][5];
int fix[5][5];
void naive() {
  assert(n <= 2);
  int a, b, c;
  for(int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--; b--;
    fix[a][b] = 1;
    f[a][b] = c;
  }
  int ans = 0;
  for(int i = 0; i < 1 << (n * n); i++) {
    int ok = 1;
    int sum = 0;
    for(int j = 0; j < (n * n); j++) {
      int y = j / n;
      int x = j % n;
      int val = !!(i & (1 << j));
      if(fix[y][x] && f[y][x] != val) ok = 0;
      sum ^= val;
    }
    if(ok && sum == 0) ans++;
  }
  cout << ans << endl;
  exit(0);
}

/// --- math {{{ ///
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d;
  return b == 0 ? (x = 1, y = 0, a) : (d = extgcd(b, a % b, y, x), y -= a / b * x, d);
}
ll modinv(ll a, ll mod) {
  ll x, y;
  extgcd(a, mod, x, y);
  if(x < 0) x += mod;
  return x;
}
ll modpow(ll a, ll b, ll mod) {
  ll r = 1;
  a %= mod;
  while(b) {
    if(b & 1) r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
/// }}}--- ///


void zero() {
  cout << 0 << endl;
  exit(0);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  for(int i = 0; i < 1 << 9; i++) checkDp[i] = check(i);
  for(int i = 0; i < 1 << 9; i++) movDp[i] = mov(i);

  cin >> n >> m;
  if(n <= 2) naive();

  int a, b, c;
  ll fre = (n * n - 5 * (n - 2) - 4) / 2;
  map<pair<int, int>, int> m2;
  for(int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--; b--;
    if(abs(a - b) >= 3) {
      auto p = make_pair(a, b);
      auto q = make_pair(b, a);
      if(m2.count(q)) {
        if(m2[q] != c) {
          zero();
        }
      } else fre--;
      m2[p] = c;
    } else {
      w[a][b - (a - 2)] = c + 1;
    }
  }

  ll ans = 0;
  for(int i = 0; i < 1 << 9; i++) ans += solve(1, i);
  ans %= mod;
  ans = modpow(2, fre, mod) * ans % mod;
  ans = (ans % mod + mod) % mod;

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


Measured memory_bytes: 613888