Problem:
Score : 900 points

Problem Statement

Takahashi has an N \times N grid. The square at the i-th row and the j-th column of the grid is denoted by (i,j).
Particularly, the top-left square of the grid is (1,1), and the bottom-right square is (N,N).

An integer, 0 or 1, is written on M of the squares in the Takahashi's grid.
Three integers a_i,b_i and c_i describe the i-th of those squares with integers written on them: the integer c_i is written on the square (a_i,b_i).

Takahashi decides to write an integer, 0 or 1, on each of the remaining squares so that the condition below is satisfied.
Find the number of such ways to write integers, modulo 998244353.

For all 1\leq i < j\leq N, there are even number of 1s in the square region whose top-left square is (i,i) and whose bottom-right square is (j,j).

Constraints

2 \leq N \leq 10^5

0 \leq M \leq min(5 \times 10^4,N^2)

1 \leq a_i,b_i \leq N(1\leq i\leq M)

0 \leq c_i \leq 1(1\leq i\leq M)

If i \neq j, then (a_i,b_i) \neq (a_j,b_j).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
:
a_M b_M c_M

Output

Print the number of possible ways to write integers, modulo 998244353.

Sample Input 1

3 3
1 1 1
3 1 0
2 3 1

Sample Output 1

8

For example, the following ways to write integers satisfy the condition:

101 111
011 111
000 011

Sample Input 2

4 5
1 3 1
2 4 0
2 3 1
4 2 1
4 4 1

Sample Output 2

32

Sample Input 3

3 5
1 3 1
3 3 0
3 1 0
2 3 1
3 2 1

Sample Output 3

0

Sample Input 4

4 8
1 1 1
1 2 0
3 2 1
1 4 0
2 1 1
1 3 0
3 4 1
4 4 1

Sample Output 4

4

Sample Input 5

100000 0

Sample Output 5

342016343

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#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 & c;
        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;
}