// includes
#include <bits/stdc++.h>
using namespace std;

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL<<(n))
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), 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;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 998244353;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20);}
} fast_io_;

template <typename T>
T power(T a, T n, T mod) {
  T res = 1;
  T tmp = n;
  T curr = a;
  while(tmp){
    if(tmp % 2 == 1){
      res = (T)(res * curr % mod);
    }
    curr = (T)(curr * curr % mod);
    tmp >>= 1;
  }

  return res;
}


map<int, int> row[100010], col[100010];
int v[4][4];
ll dp[100010][2];

int fet(int r, int c){
  if(row[r].find(c) == row[r].end())return -1;
  return row[r][c];
}

int main(int argc, char const* argv[])
{
  int n, m; cin >> n >> m;
  rep(i, 4){
    rep(j, 4)v[i][j] = -1;
  }
  rep(i, m){
    int a, b, c; cin >> a >> b >> c; a--, b--;
    row[a][b] = c;
    col[b][a] = c;
    if(a < 3 && b < 3)v[a][b] = c;
  }
  rep(i, 1 << 4){
    bool ok = true;
    int cnt = 0;
    rep(j, 2){
      rep(k, 2){
        int t = (i >> (2*j+k)) & 1;
        if(v[j][k] != -1 && t != v[j][k])ok = false;
        if(t)cnt++;
      }
    }
    if(ok && cnt % 2 == 0)dp[1][(i>>3)&1]++;
  }
  if(n == 2){
    cout << (dp[1][0] + dp[1][1]) << endl;
    return 0;
  }
  rep(i, n){
    row[i][n] = -1;
    col[i][n] = -1;
  }
  FOR(i, 2, n){
    vector<int> v(6);
    int c = 0;
    for(int k = 0; k <= 2; k++){
      for(int l = 2-k; l <= 2; l++){
        v[c] = fet(i - 2 + k, i - 2 + l);
        c++;
      }
    }
    rep(j, 1 << 6){
      int curr = 0;
      bool ok = true;
      int cnt1 = 0, cnt2 = 0;
      for(int k = 0; k <= 2; k++){
        for(int l = 2-k; l <= 2; l++){
          int f = v[curr];
          int t = (j >> curr) & 1;
          if(f != -1 && f != t)ok = false;
          if(t && !((k == 2 && l == 0) || (k == 0 && l == 2)))cnt2++;
          if(t && !(k == 1 && l == 1))cnt1++;
          curr++;
        }
      }
      if(ok && cnt1 % 2 == 0 && cnt2 % 2 == 0)(dp[i][(j>>5)&1] += dp[i-1][(j>>1)&1]) %= mod;
    }
    int rem = i - 2;
    ll tmp = 1;
    auto itr1 = row[i].begin(), itr2 = col[i].begin();
    while(true){
      if(itr1->first >= i - 2 && itr2->first >= i - 2)break;
      if(itr1->first >= i - 2){
        rem--;
        itr2++;
      }else if(itr2->first >= i - 2){
        rem--;
        itr1++;
      }else{
        if(itr1->first < itr2->first){
          rem--;
          itr1++;
        }else if(itr1->first > itr2->first){
          rem--;
          itr2++;
        }else{
          if(itr1->second == itr2->second)rem--;
          else tmp = 0;
          itr1++;
          itr2++;
        }
      }
    }
    tmp = tmp * power<ll>(2, rem, mod) % mod;
    (dp[i][0] *= tmp) %= mod;
    (dp[i][1] *= tmp) %= mod;
  }
  ll res = (dp[n-1][0] + dp[n-1][1]) % mod;
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 25216