#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)

inline int read()
{
	int res = 0; bool bo = 0; char c;
	while (((c = getchar()) < '0' || c > '9') && c != '-');
	if (c == '-') bo = 1; else res = c - 48;
	while ((c = getchar()) >= '0' && c <= '9')
		res = (res << 3) + (res << 1) + (c - 48);
	return bo ? ~res + 1 : res;
}

template <class T>
inline T Max(const T &a, const T &b) {return a > b ? a : b;}

const int N = 1e5 + 5, ZZQ = 998244353;

int n, m, a[N], b[N], c[N], f[N][2], col[N], cl[N], cr[N], sw[N];

std::map<int, int> coord[N];

int qpow(int a, int b)
{
	int res = 1;
	while (b)
	{
		if (b & 1) res = 1ll * res * a % ZZQ;
		a = 1ll * a * a % ZZQ;
		b >>= 1;
	}
	return res;
}

int main()
{
	int i;
	n = read(); m = read();
	For (i, 1, n) col[i] = cl[i] = cr[i] = -1;
	For (i, 1, m)
	{
		a[i] = read(); b[i] = read(); c[i] = read();
		coord[a[i]][b[i]] = c[i] + 1;
		if (a[i] == b[i]) col[a[i]] = c[i];
		if (a[i] + 1 == b[i]) cl[b[i]] = c[i];
		if (a[i] == b[i] + 1) cr[a[i]] = c[i];
	}
	if (col[1] != 1) f[1][0] = 1;
	if (col[1] != 0) f[1][1] = 1;
	For (i, 2, n) sw[i] = 0;
	For (i, 1, m)
	{
		if (a[i] == b[i] || a[i] + 1 == b[i] || a[i] == b[i] + 1
			|| a[i] + 2 == b[i] || a[i] == b[i] + 2) continue;
		if (!coord[b[i]][a[i]])
		{
			if (sw[Max(a[i], b[i])] != -1) sw[Max(a[i], b[i])]++;
		}
		else if (a[i] < b[i])
		{
			if (sw[Max(a[i], b[i])] != -1) sw[Max(a[i], b[i])]++;
			if (c[i] != coord[b[i]][a[i]] - 1) sw[Max(a[i], b[i])] = -1;
		}
	}
	For (i, 4, n) sw[i] = sw[i] == -1 ? 0 : qpow(2, i - 3 - sw[i]);
	For (i, 2, n)
	{
		if (col[i] != 0)
		{
			int g0 = 1, g1 = 1;
			if (coord[i][i - 1] && coord[i - 1][i]
				&& coord[i][i - 1] != coord[i - 1][i])
					g0 = 0;
			else if (!coord[i][i - 1] && !coord[i - 1][i])
				g0 = 2;
			if (i > 2)
			{
				if (coord[i][i - 2] && coord[i - 2][i]
					&& coord[i][i - 2] == coord[i - 2][i])
						g1 = 0;
				else if (!coord[i][i - 2] && !coord[i - 2][i])
					g1 = 2;
			}
			f[i][1] = (1ll * g0 * g1 * f[i - 1][1]
				% ZZQ * (i > 3 ? sw[i] : 1) + f[i][1]) % ZZQ;
			g0 = 1; g1 = 1;
			if (coord[i][i - 1] && coord[i - 1][i]
				&& coord[i][i - 1] == coord[i - 1][i])
					g0 = 0;
			else if (!coord[i][i - 1] && !coord[i - 1][i])
				g0 = 2;
			if (i > 2)
			{
				if (coord[i][i - 2] && coord[i - 2][i]
					&& coord[i][i - 2] != coord[i - 2][i])
						g1 = 0;
				else if (!coord[i][i - 2] && !coord[i - 2][i])
					g1 = 2;
			}
			f[i][1] = (1ll * g0 * g1 * f[i - 1][0]
				% ZZQ * (i > 3 ? sw[i] : 1) + f[i][1]) % ZZQ;
		}
		if (col[i] != 1)
		{
			int g0 = 1, g1 = 1;
			if (coord[i][i - 1] && coord[i - 1][i]
				&& coord[i][i - 1] == coord[i - 1][i])
					g0 = 0;
			else if (!coord[i][i - 1] && !coord[i - 1][i])
				g0 = 2;
			if (i > 2)
			{
				if (coord[i][i - 2] && coord[i - 2][i]
					&& coord[i][i - 2] == coord[i - 2][i])
						g1 = 0;
				else if (!coord[i][i - 2] && !coord[i - 2][i])
					g1 = 2;
			}
			f[i][0] = (1ll * g0 * g1 * f[i - 1][1]
				% ZZQ * (i > 3 ? sw[i] : 1) + f[i][0]) % ZZQ;
			g0 = 1; g1 = 1;
			if (coord[i][i - 1] && coord[i - 1][i]
				&& coord[i][i - 1] != coord[i - 1][i])
					g0 = 0;
			else if (!coord[i][i - 1] && !coord[i - 1][i])
				g0 = 2;
			if (i > 2)
			{
				if (coord[i][i - 2] && coord[i - 2][i]
					&& coord[i][i - 2] != coord[i - 2][i])
						g1 = 0;
				else if (!coord[i][i - 2] && !coord[i - 2][i])
					g1 = 2;
			}
			f[i][0] = (1ll * g0 * g1 * f[i - 1][0]
				% ZZQ * (i > 3 ? sw[i] : 1) + f[i][0]) % ZZQ;
		}
	}
	std::cout << (f[n][0] + f[n][1]) % ZZQ << std::endl;
	return 0;
}

Measured memory_bytes: 31872