#include <bits/stdc++.h>
#include <unistd.h>

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}


typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
//const ll MOD = 1000000007;
const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { -1,0, 1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
// #define mp make_pair
//#define endl '\n'


ll mod_pow(ll x, ll p, ll M = MOD) {
  ll a = 1;
  while (p) {
    if (p % 2)
      a = a*x%M;
    x = x*x%M;
    p /= 2;
  }
  return a;
}

ll n, m;
map<pii, int> mp;

bool check_func(int x) {
  assert(x >= 0 and x <= (1 << 9));
  int cnt = 0;
  rep (i, 9) {
    if (x & (1 << i)) cnt++;
  }
  if (cnt % 2 == 1) return false;
  if (((int)(bool)(x & (1 << 0)) + (int)(bool)(x & (1 << 1)) + (int)(bool)(x & (1 << 3)) + (int)(bool)(x & (1 << 4))) % 2 == 1) return false;
  if (((int)(bool)(x & (1 << 4)) + (int)(bool)(x & (1 << 5)) + (int)(bool)(x & (1 << 7)) + (int)(bool)(x & (1 << 8))) % 2 == 1) return false;
  return true;
}
vector<bool> check(1 << 9);

vi seni(int x) {
  vi res;
  rep (i, 1 << 9) {
    if (not check[i]) continue;
    vector<pii> p = {pii(4, 0), pii(5, 1), pii(7, 3), pii(8, 4)};
    bool flag = true;
    rep (j, p.size()) {
      if ((bool)(x & (1 << p[j].first)) != (bool)(i & (1 << p[j].second))) {
        flag = false;
        break;
      }
    }
    if (flag) res.push_back(i);
  }
  return res;
}

ll solve_dp() {
  vi valid;
  vi valid2idx(1 << 9, -1);
  rep (i, 1 << 9) {
    check[i] = check_func(i);
    if (check[i]) {
      valid.push_back(i);
      valid2idx[i] = valid.size() - 1;
    }
  }
  //DEBUG_VEC(valid);
  int vn = valid.size();
  vl dp(vn);
  vii to(vn);
  rep (i, vn) {
    int x = valid[i];
    auto temp = seni(x);
    rep (j, temp.size()) temp[j] = valid2idx[temp[j]];
    to[i] = temp;
  }
  /*
  DEBUG_VEC(valid);
  DEBUG_MAT(to);
  DEBUG_VEC(valid2idx);
  //*/
  
  rep (i, vn) {
    int ma = valid[i];
    bool flag = true;
    rep (j, 9) {
      int y0 = j / 3, x0 = j % 3;
      int y = y0 + 0, x = x0 + 0;
      if (not mp.count(pii(y, x))) {
        continue;
      }
      if (mp[pii(y, x)] != (bool)(ma & (1 << j))) {
        flag = false;
        break;
      }
    }
    dp[i] = flag;
  }
  //DEBUG_VEC(dp);
  rep (i, n - 3) {
    vl dp2(vn);
    rep (j, vn) {
      int maj = valid[j];
      if (dp[j] == 0) continue;
      rep (k, to[j].size()) {
        (dp2[to[j][k]] += dp[j]) %= MOD;
      }
    }
    rep (k, vn) {
      int mak = valid[k];
      bool flag = true;
      rep (l, 9) {
        int y0 = l / 3, x0 = l % 3;
        int y = y0 + i + 1, x = x0 + i + 1;
        if (not mp.count(pii(y, x))) {
          continue;
        }
        if (mp[pii(y, x)] != (bool)(mak & (1 << l))) {
          flag = false;
          break;
        }
      }
      if (!flag) {
        dp2[k] = 0;
      }
    }
    swap(dp, dp2);
    //DEBUG_VEC(dp);
  }

  ll res = 0;
  rep (i, vn) {
    (res += dp[i]) %= MOD;
  }
  
  return res;
}

int solve2() {
  int res = 0;
  rep (i, 1 << 4) {
    int cnt = 0;
    rep (j, 4) cnt += (bool)(i & (1 << j));
    if (cnt % 2 == 1) continue;
    bool flag = true;
    rep (j, 4) {
      int y = j / 2, x = j % 2;
      if (not mp.count(pii(y, x))) continue;
      if (mp[pii(y, x)] != bool(i & (1 << j))) {
        flag = false;
        break;
      }
    }
    res += flag;
  }
  return res;
}

int main() {
  cin >> n >> m;
  
  rep (i, m) {
    int u, v, c;
    cin >> u >> v >> c;
    u--; v--;
    mp[pii(u, v)] = c;
  }
  if (n == 2) {
    cout << solve2() << endl;
    return 0;
  }

  ll cnt;
  if (n <= 3) cnt = 0;
  else {
    cnt = (n - 3) * (n - 2) / 2;
  }
  ll ans = 1;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    int u = itr->first.first, v = itr->first.second;
    int c = itr->second;
    if (abs(u - v) <= 2) continue;
    if (mp.count(pii(v, u))) {
      if (c != mp[pii(v, u)]) ans = 0;
      else {
        if (u > v) cnt--;
      }
    }
    else {
      cnt--;
    }
  }
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }
  ans = mod_pow(2, cnt);

  //DEBUG(ans);
  ll mul = solve_dp();
  //DEBUG(mul);
  ans = ans * mul % MOD;
  //DEBUG(ans);
  cout << ans << endl;
}




Measured memory_bytes: 3456