#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 300010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 998244353;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N, M;
map<pi, int> D;
ll dp[MAX_N][2];

ll mod_pow(ll a, ll n) {
	if(n == 0) return 1;
	ll res = mod_pow(a, n / 2);
	if(n % 2 == 0) return res * res % mod;
	else return a * res % mod * res % mod;
}

void doit(int& v, int x, int y) {
	pi p = pi(x, y);
	if(D.count(p)) v = D[p];
	else v = -1;
}

void solve() {
	cin >> N >> M;
	rep(i, 0, M) {
		int a, b, c;
		cin >> a >> b >> c;
		D[pi(a, b)] = c;
	}
	ll tmp = 1ll * N * N - (4 + 5 * (N - 2));
	ll res = 1;
	for(auto pp: D) {
		pi p = pp.fst;
		int v = p.sec;
		if(abs(p.fst - p.sec) <= 2) continue;
		if(p.fst == p.sec) {
			if(v == 1) res = 0;
		}
		else {
			pi q = pi(p.sec, p.fst);
			if(D.count(q)) {
				if(D[q] != D[p]) res = 0;
				else tmp--;
			}
			else tmp -= 2;
		}
	}
	int v1, v2, v3, v4;
	doit(v1, 2, 2);
	doit(v2, 2, 1);
	doit(v3, 1, 2);
	doit(v4, 1, 1);
	rep(k1, 0, 2) {
		rep(k2, 0, 2) {
			rep(k3, 0, 2) {
				rep(k4, 0, 2) {
					if(v1 != -1 && k1 != v1) continue;
					if(v2 != -1 && k2 != v2) continue;
					if(v3 != -1 && k3 != v3) continue;
					if(v4 != -1 && k4 != v4) continue;
					if((k1 + k2 + k3 + k4) % 2) continue;
					dp[2][k1]++;
				}
			}
		}
	}
	for(int i = 2; i < N; i++) {
		rep(j, 0, 2) {
			int v1, v2, v3, v4, v5;
			doit(v1, i + 1, i + 1);
			doit(v2, i, i + 1);
			doit(v3, i + 1, i);
			doit(v4, i + 1, i - 1);
			doit(v5, i - 1, i + 1);
			rep(k1, 0, 2) {
				rep(k2, 0, 2) {
					rep(k3, 0, 2) {
						rep(k4, 0, 2) {
							rep(k5, 0, 2) {
								if(v1 != -1 && k1 != v1) continue;
								if(v2 != -1 && k2 != v2) continue;
								if(v3 != -1 && k3 != v3) continue;
								if(v4 != -1 && k4 != v4) continue;
								if(v5 != -1 && k5 != v5) continue;
								if((k1 + k2 + k3 + k4 + k5) % 2) continue;
								if((k1 + k2 + k3 + j) % 2 == 0) {
									ADD(dp[i + 1][k1], dp[i][j]);
								}
							}
						}
					}
				}
			}
		}
	}
	res = (dp[N][0] + dp[N][1]) % mod * res;
	tmp /= 2;
	res = res * mod_pow(2, tmp) % mod;
	cout << res << "\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
	srand((unsigned int)time(NULL));
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}



Measured memory_bytes: 4992