#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template<std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
using modulo = modint<998244353>;
int main() {
	int N, M;
	cin >> N >> M;
	vector<int> A(M), B(M), C(M);
	for (int i = 0; i < M; ++i) cin >> A[i] >> B[i] >> C[i], --A[i], --B[i];
	modulo mul = modulo(2).pow(N <= 3 ? 0 : 1LL * (N - 3) * (N - 2) / 2);
	map<pair<int, int>, int> d;
	for (int i = 0; i < M; ++i) {
		if (B[i] - A[i] >= 3) {
			d[make_pair(A[i], B[i])] += (C[i] + 1) * 3;
		}
		if (A[i] - B[i] >= 3) {
			d[make_pair(B[i], A[i])] += (C[i] + 1);
		}
	}
	for (pair<pair<int, int>, int> i : d) {
		if (i.second == 7 || i.second == 5) {
			mul = 0; break;
		}
		mul *= modulo(2).inv();
	}
	vector<vector<int> > val(N, vector<int>(5, -1));
	for (int i = 0; i < M; ++i) {
		if (abs(A[i] - B[i]) <= 2) {
			val[A[i]][B[i] - A[i] + 2] = C[i];
		}
	}
	function<int(int, int)> get = [&](int x, int y) {
		return val[x][y - x + 2];
	};
	vector<vector<modulo> > dp(N - 1, vector<modulo>(16));
	for (int i = 0; i < 16; ++i) {
		bool ok = true;
		int cnt = 0;
		for (int j = 0; j < 4; ++j) {
			if ((i >> j) & 1) ++cnt;
			if (((i >> j) & 1) && get(j / 2, j % 2) == 0) ok = false;
			if (!((i >> j) & 1) && get(j / 2, j % 2) == 1) ok = false;
		}
		if (ok && cnt % 2 == 0) dp[0][i] = 1;
	}
	for (int i = 1; i < N - 1; ++i) {
		for (int j = 0; j < 16; ++j) {
			for (int k = 0; k < 32; ++k) {
				bool ok = true;
				int cnt = 0;
				for (int l = 0; l < 5; ++l) {
					int tx = (l > 2 ? i - l + 3 : i + 1), ty = (l < 2 ? i + l - 1 : i + 1);
					if ((k >> l) & 1) ++cnt;
					if (((k >> l) & 1) && get(tx, ty) == 0) ok = false;
					if (!((k >> l) & 1) && get(tx, ty) == 1) ok = false;
				}
				if (ok && cnt % 2 == 0) {
					int nxt = 0, subcnt = 0;
					if ((j >> 3) & 1) nxt += 1, ++subcnt;
					if ((k >> 3) & 1) nxt += 2, ++subcnt;
					if ((k >> 1) & 1) nxt += 4, ++subcnt;
					if ((k >> 2) & 1) nxt += 8, ++subcnt;
					if (subcnt % 2 == 0) dp[i][nxt] += dp[i - 1][j];
				}
			}
		}
	}
	modulo ans = 0;
	for (int i = 0; i < 16; ++i) {
		ans += dp[N - 2][i];
	}
	cout << (ans * mul).get() << endl;
	return 0;
}

Measured memory_bytes: 19584