#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; const ll mod = 998244353; struct edge{ int v, w, nex; }e[maxn * 2]; int cnt, head[maxn], fa[maxn], col[maxn]; bool vis[maxn]; set<int> ss; int n, m; void add_edge(int u, int v, int w){ cnt++; e[cnt] = (edge){v, w, head[u]}; head[u] = cnt; } int find_fa(int x){ if(x != fa[x]) return fa[x] = find_fa(fa[x]); return x; } void join(int u, int v){ u = find_fa(u); v = find_fa(v); if(u == v) return ; fa[u] = v; } bool same(int u, int v){ u = find_fa(u); v = find_fa(v); return u == v; } void Init(){ for(int i = 1; i <= n; i++) fa[i] = i; } ll quick_mod(ll a, int n){ ll sum = 1; while(n){ if(n & 1) sum = sum * a % mod; a = a * a % mod; n >>= 1; } return sum; } bool dfs(int u, int c){ vis[u] = true; col[u] = c; for(int i = head[u]; i > 0; i = e[i].nex){ int v = e[i].v; if(!vis[v] && !dfs(v, col[u] ^ e[i].w)){ return false; } if(vis[v] && (col[u] ^ col[v]) != e[i].w) return false; } return true; } int main(){ scanf("%d%d", &n, &m); Init(); int u, v, w; int val, tot; tot = 0; while(m--){ scanf("%d%d%d", &u, &v, &w); if(w != -1) add_edge(u, v, w), add_edge(v, u, w); if(!same(u, v)){ join(u, v); } } for(int i = 1; i <= n; i++) ss.insert(find_fa(i)); //val = n - ss.size();///2^(n - 连通块个数) for(int i = 1; i <= n; i++){ if(!vis[i]){ if(!dfs(i, 0)){ printf("0\n"); return 0; } else{ tot++; } } } val = tot - ss.size(); printf("%lld\n", quick_mod((ll)2, val)); return 0; }