#define y1 askjdkasldjlkasd
#include <bits/stdc++.h>
#undef y1

using namespace std;

#define pb push_back
#define mp make_pair
#define fi(a, b) for(int i=a; i<=b; i++)
#define fj(a, b) for(int j=a; j<=b; j++)
#define fo(a, b) for(int o=a; o<=b; o++)
#define fdi(a, b) for(int i=a; i>=b; i--)
#define fdj(a, b) for(int j=a; j>=b; j--)
#define fdo(a, b) for(int o=a; o>=b; o--)
#define clr(x) memset(x, 0, sizeof(x))
#define cpy(x,y) memcpy(x, y, sizeof(y))
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

#ifdef _DEBUG
#define LOCAL
#endif

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while (0)
#endif

double START_TIME;

void exit() {
#ifdef LOCAL	
	cerr << "TIME: " << setprecision(5) << fixed << (clock() - START_TIME) / CLOCKS_PER_SEC << endl;
#endif	
	exit(0);
}

template<typename A, typename B>
ostream& operator<<(ostream& os, pair<A, B> p) {
	os << "(" << p.first << ", " << p.second << ")";
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, vector<T> v) {
	fi(0, sz(v) - 1) {
		os << v[i] << " ";
	}
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, set<T> t) {
	for (auto z : t) {
		os << z << " ";
	}
	return os;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& os, map<T1, T2> t) {
	cerr << endl;
	for (auto z : t) {
		os << "\t" << z.first << " -> " << z.second << endl;
	}
	return os;
}

#ifdef LOCAL
#define dbg(x) {cerr << __LINE__ << "\t" << #x << ": " << x << endl;}
#define dbg0(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 0; ABC < n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#define dbg1(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 1; ABC <= n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#else
#define dbg(x) while(0){}
#define dbg0(x, n) while(0){}
#define dbg1(x, n) while(0){}
#endif

#ifdef LOCAL
#define ass(x) if (!(x)) { cerr << __LINE__ << "\tassertion failed: " << #x << endl, abort(); }
#else
#define ass(x) assert(x)
#endif

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

const int MOD = 998244353;
int n;
int m;

map<pii, int> have;

int add(int a, int b) {
	a += b;
	if (a >= MOD) a -= MOD;
	return a;
}

int mul(int a, int b) {
	return (ll) a * b % MOD;
}

int bp(int x, ll d) {
	int res = 1;
	while (d) {
		if (d & 1) res = mul(res, x);
		x = mul(x, x);
		d >>= 1;
	}
	return res;
}

const int MAX = 100 * 1000 + 41;

int d[MAX][4];

bool canplace(int x, int y, int v) {
	if (!have.count(mp(x, y))) return true;
	if (!have.count(mp(y, x))) return true;
	if (x == y) {
		return have[mp(x, y)] == v;
	}
	int cur = (have[mp(x, y)] + have[mp(y, x)]) % 2;
	return (cur == v);
}

void bad() {
	printf("0\n");
	exit();
}

bool bit(int m, int i) {
	return (1 & (m >> i));
}

void solve() {
	for (auto z : have) {
		pii p = z.first;
		if (abs(p.first - p.second) >= 3) {
			if (!canplace(p.first, p.second, 0)) bad();
		}
	}
	ll deg = (ll) (1 + n - 1) * (n - 1) / 2;
	for (auto z : have) {
		pii p = z.first;
		if (p.first == p.second) continue;
		if (have.count(mp(p.second, p.first))) {
			if (p.first < p.second) {
				deg--;
			}
		} else {
			deg--;
		}
	}	

	fo(0, 3) {
		int ok = 1;
		ok &= canplace(1, 1, bit(o, 0));
		ok &= canplace(2, 2, bit(o, 1));
		int v = (bit(o, 0) ^ bit(o, 1));		
		ok &= canplace(1, 2, v);
		if (!ok) continue;
		dbg(o);
		dbg(v);
		d[2][o] = ok;
	}
	fi(3, n) {
		for (int mask = 0; mask < 8; mask++) {
			int ok = 1;
			int x1 = bit(mask, 0);
			int x2 = bit(mask, 1);
			int x3 = bit(mask, 2);
			ok &= canplace(i - 2, i - 2, x1);
			ok &= canplace(i - 1, i - 1, x2);
			ok &= canplace(i, i, x3);
			int y1 = (x1 + x2) % 2;
			int y2 = (x2 + x3) % 2;
			ok &= canplace(i - 2, i - 1, y1);
			ok &= canplace(i - 1, i, y2);
			int z1 = x2;
			ok &= canplace(i - 2, i, z1);
			if (!ok) continue;
			d[i][mask / 2] = add(d[i][mask / 2], d[i - 1][(mask & 3)]);
		}
	}
	int ans = 0;
	fo(0, 3) {
		ans = add(ans, d[n][o]);		
	}
	dbg(deg);

	ans = mul(ans, bp(2, deg));
	printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	START_TIME = (double)clock();
#endif
	
	scanf("%d %d", &n, &m);
	fi(1, m) {
		int x, y, c;
		scanf("%d %d %d", &x, &y, &c);
		have[mp(x, y)] = c;
	}
	solve();

	exit();
	return 0;
}





Measured memory_bytes: 4992