#include <bits/stdc++.h>

using namespace std;

const int md = 998244353;

inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}

inline void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += md;
}

inline int mul(int a, int b) {
  return (int) ((long long) a * b % md);
}

inline int power(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= md;
  if (a < 0) a += md;
  int b = md, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += md;
  return u;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  
  int n, m;
  cin >> n >> m;
  vector< vector< vector< pair<int, int> > > > tag(2, vector< vector< pair<int, int> > >(n + 1));
  /* position, color */

  vector<int> pw(n + 1), dp_b(n + 1), dp_w(n + 1);
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      pw[i] = 1;
    }
    pw[i + 1] = mul(pw[i], 2);
  }
  dp_b[1] = dp_w[1] = 1;

  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    tag[a < b][max(a, b)].emplace_back(min(a, b), c);
    if (a == 1 && b == 1) {
      if (c == 0) dp_b[1] = 0;
      if (c == 1) dp_w[1] = 0;
    }
  }

  vector< vector<int> > color(2, vector<int>(n + 1, -1));
  for (int i = 2; i <= n; i++) {
    /* edge -> corner, same / diff color */
    int num[3][2];
    num[0][0] = num[0][1] = max(0, i - 3);
    num[1][0] = num[1][1] = i >= 3;
    num[2][0] = num[2][1] = i >= 2;

    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < (int) tag[j][i].size(); k++) {
        int pos, c;
        tie(pos, c) = tag[j][i][k];
        color[j][pos] = c;
        if (color[j ^ 1][pos] == -1) {
          if (pos < i - 2) {
            num[0][0]--;
            num[0][1]--;
          }
          else if (pos == i - 2) {
            num[1][0]--;
            num[1][1]--;
          }
          else if (pos == i - 1) {
            num[2][0]--;
            num[2][1]--;
          }
        }
        else {
          if (pos < i - 2) {
            num[0][color[j ^ 1][pos] == c] = -1;
          }
          else if (pos == i - 2) {
            num[1][color[j ^ 1][pos] == c] = -1;
          }
          else if (pos == i - 1) {
            num[2][color[j ^ 1][pos] == c] = -1;
          }
        }
      }
    }

    if (num[0][0] >= 0 && num[1][1] >= 0 && num[2][0] >= 0) {
      add(dp_b[i], mul(dp_b[i - 1], mul(pw[num[0][0]], mul(pw[num[1][1]], pw[num[2][0]]))));
    }
    if (num[0][0] >= 0 && num[1][0] >= 0 && num[2][1] >= 0) {
      add(dp_b[i], mul(dp_w[i - 1], mul(pw[num[0][0]], mul(pw[num[1][0]], pw[num[2][1]]))));
    }
    if (num[0][0] >= 0 && num[1][1] >= 0 && num[2][1] >= 0) {
      add(dp_w[i], mul(dp_b[i - 1], mul(pw[num[0][0]], mul(pw[num[1][1]], pw[num[2][1]]))));
    }
    if (num[0][0] >= 0 && num[1][0] >= 0 && num[2][0] >= 0) {
      add(dp_w[i], mul(dp_w[i - 1], mul(pw[num[0][0]], mul(pw[num[1][0]], pw[num[2][0]]))));
    }

    if (color[0][i] == 0) dp_b[i] = 0;
    if (color[0][i] == 1) dp_w[i] = 0;

    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < (int) tag[j][i].size(); k++) {
        int pos = tag[j][i][k].first;
        color[j][pos] = -1;
      }
    }
  }

  int ans = 0;
  add(ans, dp_b[n]);
  add(ans, dp_w[n]);
  cout << ans << '\n';
  return 0;
}

Measured memory_bytes: 8920