#include using namespace std; #define rep(i, a, n) for(int i=(a); i<(n); ++i) #define per(i, a, n) for(int i=(a); i>(n); --i) #define pb emplace_back #define mp make_pair #define clr(a, b) memset(a, b, sizeof(a)) #define all(x) (x).begin(),(x).end() #define lowbit(x) (x & -x) #define fi first #define se second #define lson o<<1 #define rson o<<1|1 #define gmid l[o]+r[o]>>1 using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using ui = unsigned int; constexpr int mod = 998244353; constexpr int inf = 0x3f3f3f3f; constexpr double EPS = 1e-8; const double PI = acos(-1.0); constexpr int N = 1e5 + 10; int n, Q, m, fa[N], sz[N]; vector G; vector V[N]; int color[N]; int find_fa(int x){ return x == fa[x] ? x : (fa[x] = find_fa(fa[x])); } void join(int x, int y){ x = find_fa(x); y = find_fa(y); if(x == y) return; --m; if(sz[x] < sz[y]) swap(x, y); fa[y] = x; sz[x] += sz[y]; } bool dfs(int x){ for(int y : V[x]){ int z = find_fa(y); if(color[z]){ if(color[x] == color[z]) return 0; } else { color[z] = 3 - color[x]; if(!dfs(z)) return 0; } } return 1; } ll solve(){ for(auto [x, y] : G){ int x0 = find_fa(x); int y0 = find_fa(y); if(x0 == y0) return 0; V[x0].pb(y0); V[y0].pb(x0); } ll ans = 1; rep(i, 1, n + 1){ int x = find_fa(i); if(x != i) continue; if(color[x] == 0){ color[x] = 1; if(!dfs(x)) return 0; ans = ans * 2 % mod; } } return ans; } void _main(){ int x, y, z; cin >> n >> Q; rep(i, 1, n + 1){ fa[i] = i; sz[i] = 1; } m = n; while(Q--){ cin >> x >> y >> z; if(z == 0){ join(x, y); } else { G.pb(x, y); } } cout << solve() << '\n'; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); _main(); return 0; }