from collections import deque MOD = 998244353 N, Q = map(int, input().split()) edges = [[] for _ in range(N)] edges_c = [[] for _ in range(N)] for _ in range(Q): a, b, c = map(int, input().split()) a -= 1 b -= 1 edges[a].append(b) edges[b].append(a) edges_c[a].append(c) edges_c[b].append(c) dist = [-1] * N ans = 1 for i in range(N): if dist[i] != -1: continue ans *= 2 ans %= MOD dist[i] = 0 dq = deque() dq.append(i) while dq: now = dq.popleft() for j in range(len(edges[now])): nxt = edges[now][j] cost = dist[now] + edges_c[now][j] if dist[nxt] == -1: dist[nxt] = cost if edges_c[now][j] == 0: dq.appendleft(nxt) else: dq.append(nxt) else: if (dist[nxt] - cost) % 2: ans = 0 print(ans)