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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define MOD 998244353
//#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll po(ll a,int n){ll x=1;while(n--)x*=a;return x;}

typedef struct t{ll m,w,c;}T;
int tsort(const void*a,const void*b){
	T p=*(T*)a,q=*(T*)b;
	if(p.m<q.m)return -1;
	if(p.m>q.m)return  1;
	if(p.w<q.w)return -1;
	if(p.w>q.w)return 1;
	return 1;
}

ll x[4][2]={{1,0},{1,1},{2,2},{4,4}};
//x[i][j]=i個で和をjにする場合の数

T a[50010];
ll dp[100010][2];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	rep(i,0,m){
		int x,y,c;
		scanf("%d%d%d",&x,&y,&c);
		a[i].m=max(x,y);
		a[i].w=x+y;
		a[i].c=c;
	}

	qsort(a,m,sizeof(T),tsort);

	//番兵
	a[m].m=a[m].w=a[m].c=INF;
	a[m+1].m=a[m+1].w=a[m+1].c=INF;

	ll ptr=0;//まだ見てない最小index

	//2以下は愚直に見る
	ll c2=3,s2=0;
	while(a[ptr].m<=2){
		c2--;
		if(a[ptr].c==1)s2^=1;
		ptr++;
	}
	if(ptr>0&&a[ptr-1].w==4){
		dp[2][a[ptr-1].c]=x[c2][a[ptr-1].c^s2];
	}else{
		dp[2][0]=x[c2][s2];
		dp[2][1]=x[c2][s2^1];
	}
	
	//3以上はwが同じ所が偶数で、右下3x3だけ調べる
	rep(i,3,n+1){
		ll cnt=i-3;

		while(a[ptr].m==i&&a[ptr].w<=2*i-3){
			if(a[ptr+1].m==i&&a[ptr].w==a[ptr+1].w){
				if(a[ptr].c!=a[ptr+1].c){
					puts("0");
					return 0;
				}
				cnt--;
				ptr+=2;
			}else{
				cnt--;
				ptr++;
			}
		}
		
		//残りcnt2個でs2^(i-1,i-1)
		ll cnt2=2,s2=0;
		while(ptr<m&&a[ptr].m==i&&a[ptr].w==2*i-2){
			cnt2--;
			if(a[ptr].c==1)s2^=1;
			ptr++;
		}
		
		//残りcnt3個でs3^(i-1,i-1)^(i,i)
		ll cnt3=2,s3=0;
		while(ptr<m&&a[ptr].m==i&&a[ptr].w==2*i-1){
			cnt3--;
			if(a[ptr].c==1)s3^=1;
			ptr++;
		}
		
		if(ptr<m&&a[ptr].m==i&&a[ptr].w==2*i){
			//右下が指定されている場合
			dp[i][a[ptr].c]=(dp[i-1][0]*x[cnt2][s2^0]*x[cnt3][a[ptr].c^0^s3]+dp[i-1][1]*x[cnt2][s2^1]*x[cnt3][a[ptr].c^1^s3])*pom(2,cnt,MOD)%MOD;
			ptr++;
		}else{
			dp[i][0]=(dp[i-1][0]*x[cnt2][s2^0]*x[cnt3][0^0^s3]+dp[i-1][1]*x[cnt2][s2^1]*x[cnt3][0^1^s3])*pom(2,cnt,MOD)%MOD;
			dp[i][1]=(dp[i-1][0]*x[cnt2][s2^0]*x[cnt3][1^0^s3]+dp[i-1][1]*x[cnt2][s2^1]*x[cnt3][1^1^s3])*pom(2,cnt,MOD)%MOD;
		}
	}
	printf("%lld",(dp[n][0]+dp[n][1])%MOD);
}