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: Accepted)

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 998244353;
int p2(int a) {
	if (a == 0)return 1;
	int res = p2(a / 2);
	res = (res*res) % MOD;
	if (a % 2 == 1)res = (res * 2) % MOD;
	return res;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	vector<int> A(N);
	int a, b, c;
	map<pair<int, int>, int> mp;
	map<pair<int, int>, int> kakutei;
	pair<int, int> p;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> c;
		a--;
		b--;
		if (a > b)swap(a, b);
		p.first = a; p.second = b;
		if (mp.count(p) == 0) {
			mp[p] = c;
		}
		else {
			kakutei[p] = (mp[p] + c) % 2;
		}
	}
	int t = N - 3;
	int num = t*(t + 1) / 2;

	for (auto m : kakutei) {
		if (m.first.second - m.first.first >= 3) {
			if (m.second == 1) {
				cout << 0 << endl;
				return 0;
			}
		}
	}
	for (auto m : mp) {
		if (m.first.second - m.first.first >= 3) {
			num--;
		}
	}
	int res = p2(num);
	//cerr << res << " " << num << endl;

	vector<vector<int> > dp(N, vector<int>(2, 0));
	p.first = 0; p.second = 0;
	if (mp.count(p) == 0) {
		dp[0][0] = 1;
		dp[0][1] = 1;
	}
	else {
		dp[0][mp[p]] = 1;
	}
	mp[make_pair(N - 2, N)] = 0;
	int tpr;
	int tmn;
	int tmx;
	for (int i = 1; i < N; i++) {
		int mn = 0;
		int mx = 1;
		int pr = 1;
		p.first = i; p.second = i;

		if (mp.count(p) == 0) {
		}
		else {
			t = mp[p];
			mn = max(mn, t);
			mx = min(mx, t);
		}

		p.first = i - 1; p.second = i + 1;
		if (mp.count(p) == 0) {
			pr *= 2;
		}
		else {
			if (kakutei.count(p) == 0) {
				//pr *= 1;
			}
			else {
				t = kakutei[p];
				mn = max(mn, t);
				mx = min(mx, t);
			}
		}

		tpr = pr;
		tmn = mn;
		tmx = mx;
		for (int from = 0; from < 2; from++) {
			if (dp[i - 1][from] != 0) {
				pr = tpr;
				mn = tmn;
				mx = tmx;
				p.first = i - 1; p.second = i;
				if (mp.count(p) == 0) {
					pr *= 2;
				}
				else {
					if (kakutei.count(p) == 0) {
						//pr *= 1;
					}
					else {
						t = (from + kakutei[p]) % 2;
						mn = max(mn, t);
						mx = min(mx, t);
					}
				}


				for (int to = mn; to <= mx; to++) {
					dp[i][to] = (dp[i][to] + ((pr * dp[i - 1][from]) % MOD)) % MOD;
				}
			}
		}
	}
	res = (((dp[N - 1][0] + dp[N - 1][1]) % MOD) * res) % MOD;
	cout << res << endl;
}