#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
const int MOD = 998244353;
int m, a, b, c, d[100005], u, k;
ll n, t;
map<ll, int> M;
void as(int a, int b, int c){
	k = M[t = a*n+b] != c;
	if(!M[t] && (M[t] = c)) u++;
	else if(a == 2) as(0,b+1,k+1);
	else if(a == 1) d[b] = k+1;
	else if(k){
		puts("0");
		exit(0);
	}
}
ll exp(ll b, ll n){
	ll res = 1;
	for(; n; n/=2,(b*=b)%=MOD) if(n%2) (res *= b) %= MOD;
	return res;
}
int main(){
	scanf("%lld%d", &n, &m);
	rep(i,m){
		scanf("%d%d%d", &a, &b, &c);
		if(a<b) swap(a, b);
		as(a-b,b-1,c+1);
	}
	rep(i,n) if(d[i] && M[i]){
		as(0,i+1,(M[i]!=d[i])+1);
		d[i] = 0;
	}
	for(int i = n-2; i >= 0; i--) if(d[i] && M[i+1]){
		as(0,i,(M[i+1]!=d[i])+1);
		d[i] = 0;
	}
	rep(i,n) if(d[i]) u++;
	printf("%lld\n", exp(2,n*(n+1)/2-u));
}