#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
 
#define mod 998244353
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
long Pow(long X,long R){
	if(R == 0){
		return 1;
	}
	if(R % 2 == 1){
		return Pow(X,R-1)*X%mod;
	}
	else{
		long t = Pow(X,R/2);
		return t*t%mod;
	}
}
long DPO[100000];
long DPE[100000];
long A[100000];
long B[100000];
int C[100000];
void Calc(){
	int N = rei();
	int M = rei();
	map<long,int> mp;
	for(int i=0;i<M;i++){
		long a = rel()-1;
		long b = rel()-1;
		int c = rei()+1;
		mp[a*N+b] = c;
		A[i] = a;
		B[i] = b;
		C[i] = c;
	}
	int single = 0;
	int doubl = 0;
	for(int i=0;i<M;i++){
		long x = A[i];
		long y = B[i];
		if(max(x-y,y-x) > 2){
			if(mp.count(y*N+x) == 0){
				single++;
			}
			else if(mp[y*N+x] != C[i]){
				cout << 0 << endl;
				return;
			}
			else{
				doubl++;
			}
		}
	}
	long pair = N;
	pair *= N;
	for(int i=0;i<N;i++){
		pair -= min(N-1,i+2) - max(0,i-2) + 1;
	}
	pair /= 2;
	pair -= single + doubl / 2;
	long ans = Pow(2,pair);
	if(mp[N+1] != 2){
		for(int b=0;b<8;b++){
			bool even = true;
			if(b & 1){
				if(mp[0] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[0] == 2){
					continue;
				}
			}
			if(b & 2){
				if(mp[1] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[1] == 2){
					continue;
				}
			}
			if(b & 4){
				if(mp[N] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[N] == 2){
					continue;
				}
			}
			if(even){
				DPE[1]++;
			}
		}
	}
	if(mp[N+1] != 1){
		for(int b=0;b<8;b++){
			bool even = false;
			if(b & 1){
				if(mp[0] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[0] == 2){
					continue;
				}
			}
			if(b & 2){
				if(mp[1] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[1] == 2){
					continue;
				}
			}
			if(b & 4){
				if(mp[N] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[N] == 2){
					continue;
				}
			}
			if(even){
				DPO[1]++;
			}
		}
	}
	for(long i=2;i<N;i++){
		int even1 = 0;
		int even2 = 0;
		int odd1 = 0;
		int odd2 = 0;
		for(int b=0;b<4;b++){
			bool even = true;
			if(b & 1){
				if(mp[(i-1)*N+i] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[(i-1)*N+i] == 2){
					continue;
				}
			}
			if(b & 2){
				if(mp[i*N+i-1] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[i*N+i-1] == 2){
					continue;
				}
			}
			if(even){
				even1++;
			}
			else{
				odd1++;
			}
		}
		for(int b=0;b<4;b++){
			bool even = true;
			if(b & 1){
				if(mp[(i-2)*N+i] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[(i-2)*N+i] == 2){
					continue;
				}
			}
			if(b & 2){
				if(mp[i*N+i-2] == 1){
					continue;
				}
				even = !even;
			}
			else{
				if(mp[i*N+i-2] == 2){
					continue;
				}
			}
			if(even){
				even2++;
			}
			else{
				odd2++;
			}
		}
		if(mp[i*N+i] != 1){
			DPO[i] = DPE[i-1]*odd1*even2 + DPO[i-1]*even1*odd2;
			DPO[i] %= mod;
		}
		if(mp[i*N+i] != 2){
			DPE[i] = DPE[i-1]*even1*even2 + DPO[i-1]*odd1*odd2;
			DPE[i] %= mod;
		}
	}
	ans *= DPE[N-1] + DPO[N-1];
	ans %= mod;
	cout << ans << endl;
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}

Measured memory_bytes: 37248