#include using namespace std; using Vi = std::vector ; using VVi = vector; int par[200020], siz[200020]; void init(int N) { iota(par, par + N, 0); fill(siz, siz + N, 1); } int root(int a) { return (a == par[a] ? a : root(par[a])); } void merge(int a, int b) { a = root(a); b = root(b); if (a == b) { return; } if (siz[a] < siz[b]) { swap(a, b); } par[b] = a; siz[a] += siz[b]; } int main() { int N, Q; cin >> N >> Q; vector> X; for (int i = 0; i < Q; i ++) { int a, b, c; cin >> a >> b >> c; if (c) { X.emplace_back(--a, --b); } else { merge(--a, --b); } } VVi gr(N); for (auto [a, b] : X) { a = root(a); b = root(b); gr[a].push_back(b); gr[b].push_back(a); } using ll = long long; ll const m = 998244353; ll ans = 1; int did[20002]; fill(did, did + N, -1); for (int i = 0; i < N; i ++) { if (i != root(i) || did[i] != -1) { continue; } int ok = 2; queue que; did[i] = 0; que.push(i); while (!que.empty()) { int u = que.front(); que.pop(); for (auto v : gr[u]) { if (did[v] == -1) { did[v] = 1 - did[u]; que.push(v); } else if (did[v] == did[u]) { ok = 0; } } } ans = (ans * ok) % m; } cout << ans << endl; }