#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 4000001;
const double eps = 1E-18;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint operator^(const ll& t)const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

///////////////////////////

vector<vector<edge>> g;
vector<ll> diag;

void dfs(ll v) {
	for (edge e : g[v]) {
		ll w = e.to;
		if (diag[w] != -1) { continue; }
		diag[w] = (diag[v] + e.d) % 2;
		dfs(w);
	}
}


int main() {
	ll N, M;
	cin >> N >> M;

	map<pair<ll, ll>, ll> mp;

	repn(i, M) {
		ll a, b, c;
		cin >> a >> b >> c;
		mp[{a, b}] = c;

		if (!mp.count({ b,a })) { mp[{b, a}] = -1; }
	}

	mint ans = 1;

	g.resize(N + 1);
	diag.assign(N + 1, -1);

	for (auto p : mp) {
		ll a = p.first.first;
		ll b = p.first.second;

		ll x = mp[{a, b}];
		ll y = mp[{b, a}];
		if (x == -1 || y == -1) { continue; }
		
		if (a < b) { continue; }

		if (a == b) { diag[a] = x; }
		else if (a - b == 1) {
			ll z = (x + y) % 2;
			g[a].push_back({ 0,a,b,z });
			g[b].push_back({ 0,b,a,z });
		}
		else if (a - b == 2) {
			ll z = (x + y) % 2;
			diag[a - 1] = z;
		}
	}

	//repn(i, N) cout << diag[i] << " ";
	//cout << endl;

	repn(v, N) {
		if (diag[v] != -1) { dfs(v); }
	}
	
	repn(v, N) {
		if (diag[v] != -1) { continue; }
			ans *= 2;
			diag[v] = 0;
		
		dfs(v);
	}

	//cout << ans << endl;

	for (auto p : mp) {
		ll a = p.first.first;
		ll b = p.first.second;

		ll x = mp[{a, b}];
		ll y = mp[{b, a}];
		if (x == -1 || y == -1) { continue; }

		if (a < b) { continue; }

		if (a == b) {
			if (diag[a] != x) { ans = 0; }
		}
		else if (a - b == 1) {
			ll z = (x + y) % 2;
			if ((diag[a] + diag[b]) % 2 != z) { ans = 0; }
		}
		else if (a - b == 2) {
			ll z = (x + y) % 2;
			if (diag[a - 1] != z) { ans = 0; }
		}
		else {
			ll z = (x + y) % 2;
			if (z % 2 != 0) { ans = 0; }
		}

		//cout << a << " " << b << " " << ans << endl;
	}

	ll use = mp.size();
	repn(i, N) { if (!mp.count({i,i})) { use++; } }
	ll remain = (N * N - use) / 2;
	ans *= (mint)2 ^ remain;

	cout << ans << endl;
	
	
}


Measured memory_bytes: 9600