#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (int i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define int long long
using namespace std;
using II = pair<int, int>; using VII = vector<II>; using VVII = vector<VII>;
using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>;
template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class T> ostream& operator<<(ostream &s, const vector<T>& d) { int n = d.size(); REP (i, n) s << d[i] << " "; return s; }
template <class T> ostream& operator<<(ostream &s, const vector<vector<T>>& dd) { for (vector<T> d: dd) s << d << endl; return s; }
template <class T, class S> ostream& operator<<(ostream &s, const pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream &s, const map<T, S> m) { for (pair<T, S> p: m) s << p << endl; return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "\t" << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl << "\t", dump_func(__VA_ARGS__);
#else
  #define dump(...)
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }
struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
constexpr int MOD = 998244353;

int mod_pow(int x, int n, int mod = MOD) {
  if (n <= 0) return 1;
  int tmp = mod_pow(x, n / 2, mod);
  return tmp * tmp % mod * (n % 2 ? x : 1) % mod;
}

void fin() {
  cout << 0 << endl; exit(0);
}
signed main() {
  int N, M; cin >> N >> M;
  map<II, bool> t;
  REP (i, M) {
    int a, b, c; cin >> a >> b >> c;
    a--;
    b--;
    t[{a, b}] = c;
  }
  // コーナーケース
  if (N == 1) {
    if (t[{0, 0}] == 1) {
      fin();
    } else {
      cout << 1 << endl; return 0;
    }
  }
  if (N == 2) {
    int ans = 0;
    VI xs = {0, 1, 0, 1};
    VI ys = {0, 0, 1, 1};
    REP (k, 1 << 4) {
      if (__builtin_popcount(k) % 2) continue;
      bool ok = true;
      REP (j, 4) {
        int c = k >> j & 1;
        II pos = {xs[j], ys[j]};
        if (t.count(pos) && t[pos] != c) {
          ok = false; break;
        }
      }
      if (ok) ans++;
    }
    cout << ans << endl; return 0;
  }

  // 3 * 3以外
  int cnt = max(0LL, (N - 3) * (N - 2)) / 2; // 自由度
  for (auto& p: t) {
    int x = p.fi.fi;
    int y = p.fi.se;
    if (-2 <= x - y && x - y <= 2) continue;
    II op = {y, x};
    if (t.count(op)) {
      if (p.se == t[op]) {
        t.erase(op);
        cnt--;
      } else {
        fin();
      }
    } else {
      cnt--;
    }
  }
  assert(cnt >= 0);

  // 3 * 3
  VI bef(1 << 4, 1);
  const VI xs = {0, 1, 2, 0, 1, 2, 0, 1, 2};
  const VI ys = {0, 0, 0, 1, 1, 1, 2, 2, 2};
  REP (i, N - 2) {
    VI nxt(1 << 4);
    REP (k, 1 << 9) {
      // 3 * 3
      if (__builtin_popcount(k) % 2) continue;
      // 右下 2 * 2
      if (__builtin_popcount(k & 0b110110000) % 2) continue;
      // 左上 2 * 2
      if (__builtin_popcount(k & 0b000011011) % 2) continue;
      int ok = true;
      REP (j, 9) {
        bool c = k >> j & 1;
        II pos = {i + xs[j], i + ys[j]};
        if (t.count(pos) && t[pos] != c) {
          ok = false; break;
        }
      }
      if (!ok) continue;
      int bit = 0;
      bit |= (k >> 4 & 1) << 0;
      bit |= (k >> 5 & 1) << 1;
      bit |= (k >> 7 & 1) << 2;
      bit |= (k >> 8 & 1) << 3;
      (nxt[bit] += bef[(k & 0b11000) >> 1 | k & 0b11]) %= MOD;
    }
    bef = move(nxt);
  }
  int ans = 0;
  REP (i, 1 << 4) {
    (ans += bef[i]) %= MOD;
  }
  (ans *= mod_pow(2, cnt)) %= MOD;
  cout << ans << endl;
}


Measured memory_bytes: 3456