#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<unsigned P> struct ModInt {
  using M = ModInt;
  unsigned v;
  ModInt() : v(0) {}
  template<class Z> ModInt(Z x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
  constexpr ModInt(unsigned v, int) : v(v) {}
  static constexpr unsigned p() { return P; }
  M operator+() const { return *this; }
  M operator-() const { return {v ? P - v : 0, 0}; }
  explicit operator bool() const noexcept { return v; }
  bool operator!() const noexcept { return !(bool) *this; }
  M operator*(M r) const { return M(*this) *= r; }
  M operator/(M r) const { return M(*this) /= r; }
  M operator+(M r) const { return M(*this) += r; }
  M operator-(M r) const { return M(*this) -= r; }
  bool operator==(M r) const { return v == r.v; }
  bool operator!=(M r) const { return !(*this == r); }
  M& operator*=(M r) { v = (uint64_t) v * r.v % P; return *this; }
  M& operator/=(M r) { return *this *= r.inv(); }
  M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; }
  M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; }
  M inv() const {
    int a = v, b = P, x = 1, u = 0;
    while (b) {
      int q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * u, u);
    }
    assert(a == 1);
    return x;
  }
  template<class Z> M pow(Z n) const {
    if (n < 0) return pow(-n).inv();
    M res = 1;
    for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;
    return res;
  }
  template<class Z> friend M operator*(Z l, M r) { return M(l) *= r; }
  template<class Z> friend M operator/(Z l, M r) { return M(l) /= r; }
  template<class Z> friend M operator+(Z l, M r) { return M(l) += r; }
  template<class Z> friend M operator-(Z l, M r) { return M(l) -= r; }
  friend ostream& operator<<(ostream& os, M r) { return os << r.v; }
  friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; }
  template<class Z> friend bool operator==(Z l, M r) { return M(l) == r; }
  template<class Z> friend bool operator!=(Z l, M r) { return !(l == r); }
};
using Mint = ModInt<998244353>;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, m; cin >> n >> m;
  auto _ = [&](int i, int j) -> int { return 4 * i + j + 2; };
  V<> a(5 * n, -1);
  map< pair<int, int>, bool > mp;
  lint cnt = (lint) (n - 2) * (n - 3) / 2;
  while (m--) {
    int i, j, c; cin >> i >> j >> c, --i, --j;
    if (abs(j - i) < 3) {
      a[_(i, j)] = c;
    } else {
      mp[{i, j}] = c;
      if (mp.count({j, i})) {
        if(mp[{i, j}] != mp[{j, i}]) return cout << 0 << '\n', 0;
        continue;
      }
      --cnt;
    }
  }
  auto fn = [](int bit, int nbit) -> int {
    int res = 0;
    if (bit & 1 << 0) res |= 1 << 0;
    if (bit & 1 << 1) res |= 1 << 1;
    if (nbit & 1 << 3) res |= 1 << 2;
    if (bit & 1 << 2) res |= 1 << 3;
    if (bit & 1 << 3) res |= 1 << 4;
    if (nbit & 1 << 0) res |= 1 << 5;
    if (nbit & 1 << 4) res |= 1 << 6;
    if (nbit & 1 << 1) res |= 1 << 7;
    if (nbit & 1 << 2) res |= 1 << 8;
    return res;
  };
  auto chk = [&](int s, int bit) -> bool {
    if (__builtin_popcount(bit & 27) & 1) return false;
    if (__builtin_popcount(bit & 432) & 1) return false;
    if (__builtin_popcount(bit) & 1) return false;
    for (int k = 0; k < 9; ++k) {
      int i = s + k / 3;
      int j = s + k % 3;
      if (i >= n or j >= n) continue;
      if (a[_(i, j)] + !!(bit & 1 << k) == 1) return false;
    }
    return true;
  };
  V<Mint> dp(16);
  for (int bit = 0; bit < 16; ++bit) for (int nbit = 0; nbit < 32; ++nbit) {
    if (chk(0, fn(bit, nbit))) {
      dp[bit] = 1;
    }
  }
  for (int i = 2; i < n; ++i) {
    V<Mint> ndp(16);
    for (int bit = 0; bit < 16; ++bit) if (dp[bit]) {
      for (int nbit = 0; nbit < 32; ++nbit) {
        if (!chk(i - 2, fn(bit, nbit))) continue;
        ndp[(bit >> 3 | nbit << 1) & 15] += dp[bit];
      }
    }
    swap(dp, ndp);
  }
  Mint res = accumulate(begin(dp), end(dp), Mint(0));
  res *= Mint(2).pow(cnt);
  cout << res << '\n';
}

Measured memory_bytes: 5376