#include<bits/stdc++.h>
using namespace std;
#define i64 long long int
#define pii pair<int,int>
#define mp make_pair
int n;
i64 res;
map<pii,int> s;
int mul(int x,int y){
	return (int)((i64)x*y%998244353);
}
int chk(vector<pii> c){
	int f=0,cnt=0;
	for(auto X : c){
		if(!s.count(X))cnt++;else
		f^=s[X];
	}
	if(cnt==0)return f==0?1:-1;
	if(cnt==1){
		for(auto X : c)
			if(!s.count(X)){
				s[X] = f;
				return 0;
			}
	}
	return 0;
}
int calc(){
	int m;
	scanf("%d%d",&n,&m);
	i64 res = (i64)(n - 1) + (i64)(n-1) * (n-2) / 2 + m;
	while(m --){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		s[mp(x,y)] = z;
	}
	for(int i=1; i+2 <= n; i++){
		vector<pii> v;
		v.push_back(mp(i,i+2));
		v.push_back(mp(i+1,i+1));
		v.push_back(mp(i+2,i));
		int w = chk(v);
		if(w == 1)res --;else
		if(w == -1)return 0;
	}
	for(int i=1; i+1<=n; i++){
		vector<pii> v;
		v.push_back(mp(i,i));
		v.push_back(mp(i,i+1));
		v.push_back(mp(i+1,i));
		v.push_back(mp(i+1,i+1));
		int w = chk(v);
		if(w == 1)res --;else
		if(w == -1)return 0;
	}
	for(auto X : s){
		pii coord = X.first;
		if(coord.second - coord.first <= 2)continue;
		vector<pii> v;
		v.push_back(coord);
		swap(coord.first, coord.second);
		v.push_back(coord);
		int w = chk(v);
		if(w == 1)res --;else
		if(w == -1)return 0;
	}
	res = (i64)n * n - res;
	int A = 1, B = 2;
	while(1){
		if(res%2)A = mul(A, B);
		if(res/=2)B = mul(B, B);else
			break;
	}
	return A;
}
int main(){
	printf("%d\n",calc());
	return 0;
}
