#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } constexpr ll mod = 998244353; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n,q; cin >> n >> q; vector>> g(n); while (q--) { int a,b,c; cin >> a >> b >> c; a--; b--; g[a].push_back({b,c}); g[b].push_back({a,c}); } ll res = 1; vector col(n, -1); for (int i = 0; i < n; ++i) { if (col[i] != -1) continue; res *= 2; res %= mod; col[i] = 0; auto dfs = [&](auto dfs,int s) -> void { for (auto t:g[s]) { if (col[t.first] == -1) { col[t.first] = col[s]^t.second; dfs(dfs, t.first); } else { if (col[t.first] != (col[s]^t.second)) res = 0; } } }; dfs(dfs,i); } cout << res << endl; }