#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 998244353
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,m;
vector<pair<P,int> >vec,vec2;
ll modpow(ll x,ll n){
    n %= (mod-1);
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
bool used[50005];
ll dp[100005][2];
int main(){
	scanf("%d%d",&n,&m);
	rep(i,m){
		int x,y,z; scanf("%d%d%d",&x,&y,&z);
		if(abs(x-y) >= 3) vec.pb(mp(mp(x,y),z));
		else vec2.pb(mp(mp(x,y),z));
	}
	SORT(vec); SORT(vec2);
	int all = vec.size();
	ll ans = 1;
    ll free = 1LL*(n-2)*(n-3)/2;
	for(int i=0;i<vec.size();i++){
		if(used[i]) continue;
		int x = vec[i].fi.fi, y = vec[i].fi.sc,z = vec[i].sc;;
		if(POSL(vec,mp(mp(y,x),-1)) < vec.size() && vec[POSL(vec,mp(mp(y,x),-1))].fi == mp(y,x)){
			pair<P,int>q = vec[POSL(vec,mp(mp(y,x),-1))];
			int xx = q.fi.fi, yy = q.fi.sc, zz = q.sc;
			if(z+zz == 1){
				puts("0") ; return 0;
			}
			used[i] = 1;
			used[POSL(vec,mp(mp(y,x),-1))] = 1;
		}
		else{
			used[i] = 1;
		}
		free--;
	}
	ans = modpow(2LL,(free)); //cout<<ans<<endl;
	vec.clear(); vec = vec2;
	//0 and 1
	if(POSL(vec,mp(mp(1,1),-1)) < vec.size() && vec[POSL(vec,mp(mp(1,1),-1))].fi == mp(1,1)){
		int z = vec[POSL(vec,mp(mp(1,1),-1))].sc; //cout<<z<<endl;
		dp[1][z] = 1;
	}
	else{
		dp[1][0] = dp[1][1] = 1;
	}
	//cout<<dp[1][0]<<endl;
	for(int i=1;i<n;i++){
		for(int j=0;j<2;j++){
			if(dp[i][j] == 0) continue;
			int need = -1;
			if(POSL(vec,mp(mp(i+1,i+1),-1)) < vec.size() && vec[POSL(vec,mp(mp(i+1,i+1),-1))].fi == mp(i+1,i+1)){
				need = vec[POSL(vec,mp(mp(i+1,i+1),-1))].sc;
			}
			for(int k=0;k<2;k++){
				if(need != -1 && need != k) continue;
				int zan = (j^k);
				int aa = -1, bb = -1;
				if(POSL(vec,mp(mp(i,i+1),-1)) < vec.size() && vec[POSL(vec,mp(mp(i,i+1),-1))].fi == mp(i,i+1)){
					aa = vec[POSL(vec,mp(mp(i,i+1),-1))].sc;
				}
				if(POSL(vec,mp(mp(i+1,i),-1)) < vec.size() && vec[POSL(vec,mp(mp(i+1,i),-1))].fi == mp(i+1,i)){
					bb = vec[POSL(vec,mp(mp(i+1,i),-1))].sc;
				}
				for(int c=0;c<2;c++)for(int d=0;d<2;d++){
					if(aa!=-1&&aa!=c) continue;
					if(bb!=-1&&bb!=d) continue;
					if((c^d) != zan) continue;
					if(i==n-1){
						dp[i+1][k] += dp[i][j];
						if(dp[i+1][k]>=mod) dp[i+1][k]-=mod;
					}
					else{
						int aaa = -1, bbb = -1;
						if(POSL(vec,mp(mp(i,i+2),-1)) < vec.size() && vec[POSL(vec,mp(mp(i,i+2),-1))].fi == mp(i,i+2)){
							aaa = vec[POSL(vec,mp(mp(i,i+2),-1))].sc;
						}
						if(POSL(vec,mp(mp(i+2,i),-1)) < vec.size() && vec[POSL(vec,mp(mp(i+2,i),-1))].fi == mp(i+2,i)){
							bbb = vec[POSL(vec,mp(mp(i+2,i),-1))].sc;
						}
						rep(e,2)rep(f,2){
							if(aaa!=-1&&aaa!=e) continue;
							if(bbb!=-1&&bbb!=f) continue;
							if((k^e^f) == 1) continue;
							dp[i+1][k] += dp[i][j];
							if(dp[i+1][k]>=mod) dp[i+1][k]-=mod;
						
						}
					}
				}
			}
		}
	}
	ll ans2 = (dp[n][0]+dp[n][1])%mod;
	cout<<(ans*ans2%mod+mod)%mod<<endl;
}

Measured memory_bytes: 3064