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 <iostream>
#include <map>
#include <cassert>
using namespace std;

long long N, M, A[100009], B[100009], C[100009], D1[100009], D2[100009], D3[100009], dp[100009][8], mod = 998244353;
map<pair<long long, long long>, long long> Map;

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 60; i++) {
		if ((b / (1LL << i)) % 2 == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--;
		if (C[i] == 0) Map[make_pair(A[i], B[i])] = 1;
		if (C[i] == 1) Map[make_pair(A[i], B[i])] = 2;
	}
	if (N == 2) {
		int cnt = 0;
		for (int i = 0; i < 16; i++) {
			int c[2][2] = { {(i / 8), (i / 4) % 2}, {(i / 2) % 2, i % 2} };
			bool flag = true;
			for (int j = 0; j < M; j++) {
				if (c[A[j]][B[j]] != C[j]) flag = false;
			}
			if (flag == true && (c[0][0] + c[0][1] + c[1][0] + c[1][1]) % 2 == 0) {
				cnt++;
			}
		}
		cout << cnt << endl;
		return 0;
	}
	bool flag = false;
	for (int i = 0; i < M; i++) {
		if (Map[make_pair(A[i], B[i])] == 3) flag = true;
	}
	if (flag == true) {
		cout << "0" << endl;
		return 0;
	}
	for (int i = 0; i < N; i++) D1[i] = Map[make_pair(i, i)];
	for (int i = 0; i < N - 1; i++) {
		if (Map[make_pair(i, i + 1)] >= 1 && Map[make_pair(i + 1, i)] >= 1) D2[i] = (Map[make_pair(i, i + 1)] + Map[make_pair(i + 1, i)]) % 2 + 1;
	}
	for (int i = 0; i < N - 2; i++) {
		if (Map[make_pair(i, i + 2)] >= 1 && Map[make_pair(i + 2, i)] >= 1) D3[i] = (Map[make_pair(i, i + 2)] + Map[make_pair(i + 2, i)]) % 2 + 1;
	}

	dp[0][0] = 1;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 8; j++) {
			int bit[3] = { j / 4, (j / 2) % 2, j % 2 };

			// 0 にする場合
			if (D1[i] == 0 || D1[i] == 1) {
				int bit1[3] = { 0, bit[0], bit[1] };

				bool ok1 = true; if (i >= 1 && D2[i - 1] >= 1 && (bit1[0] + bit1[1] + (D2[i - 1] - 1)) % 2 == 1) ok1 = false;
				bool ok2 = true; if (i >= 2 && D3[i - 2] >= 1 && (2 * bit1[0] + 3 * bit1[1] + 2 * bit1[2] + (D3[i - 2] - 1)) % 2 == 1) ok2 = false;
				if (ok1 == true && ok2 == true) {
					dp[i + 1][4 * bit1[0] + 2 * bit1[1] + 1 * bit1[2]] += dp[i][j];
					dp[i + 1][4 * bit1[0] + 2 * bit1[1] + 1 * bit1[2]] %= mod;
				}
			}
			// 1 にする場合
			if (D1[i] == 0 || D1[i] == 2) {
				int bit2[3] = { 1, bit[0], bit[1] };
				
				bool ok1 = true; if (i >= 1 && D2[i - 1] >= 1 && (bit2[0] + bit2[1] + (D2[i - 1] - 1)) % 2 == 1) ok1 = false;
				bool ok2 = true; if (i >= 2 && D3[i - 2] >= 1 && (2 * bit2[0] + 3 * bit2[1] + 2 * bit2[2] + (D3[i - 2] - 1)) % 2 == 1) ok2 = false;
				if (ok1 == true && ok2 == true) {
					dp[i + 1][4 * bit2[0] + 2 * bit2[1] + 1 * bit2[2]] += dp[i][j];
					dp[i + 1][4 * bit2[0] + 2 * bit2[1] + 1 * bit2[2]] %= mod;
				}
			}
		}
	}

	// 最終計算
	long long sum = 0;
	for (int i = 0; i < 8; i++) sum += dp[N][i];
	sum %= mod;

	for (int i = 0; i < N - 1; i++) {
		if (Map[make_pair(i, i + 1)] == 0 && Map[make_pair(i + 1, i)] == 0) sum *= 2;
		sum %= mod;
	}
	for (int i = 0; i < N - 2; i++) {
		if (Map[make_pair(i, i + 2)] == 0 && Map[make_pair(i + 2, i)] == 0) sum *= 2;
		sum %= mod;
	}
	long long rem = (N - 2) * (N - 3) / 2;
	for (int i = 0; i < M; i++) {
		if (abs(A[i] - B[i]) >= 3) rem--;
	}
	sum *= modpow(2, rem, mod);
	sum %= mod;
	//assert(sum != 0);
	cout << sum << endl;
	return 0;
}