#include #include using mint = atcoder::modint998244353; using namespace std; int main() { int n, m; cin >> n >> m; atcoder::dsu uf(n); vector>> g(n); for (int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; g[a].push_back({b, c}); uf.merge(a, b); } mint ans = 1; vector f(n, -1); vector f2(n, true); for (auto s : uf.groups()){ int ok = 2; for (int i : s){ if (f[i] != -1){ continue; } queue q; q.push(i); f[i] = 0; while (!q.empty()){ int v = q.front(); q.pop(); for (auto p : g[v]){ if (f[p.first] == -1){ f[p.first] = (f[v] ^ p.second); q.push(p.first); }else{ if (f[p.first] != (f[v] ^ p.second)){ ok = 0; } } } } if (ok == 0){ ok = 2; f[i] = 1; q.push(i); while (!q.empty()){ int v = q.front(); q.pop(); for (auto p : g[v]){ if (f2[p.first]){ f[p.first] = (f[v] ^ p.second); q.push(p.first); }else{ if (f[p.first] != (f[v] ^ p.second)){ ok = 0; } } f2[p.first] = false; } } } if (ok == 0){ break; } } ans *= ok; } cout << ans.val() << endl; }