import sys, random input = lambda : sys.stdin.readline().rstrip() write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x)) debug = lambda x: sys.stderr.write(x+"\n") YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18 LI = lambda : list(map(int, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()] def debug(_l_): for s in _l_.split(): print(f"{s}={eval(s)}", end=" ") print() def dlist(*l, fill=0): if len(l)==1: return [fill]*l[0] ll = l[1:] return [dlist(*ll, fill=fill) for _ in range(l[0])] class UF: # unionfind def __init__(self, n): self.n = n self.parent = list(range(n)) self.size = [1] * n # self.es = [0] * n def check(self): return [self.root(i) for i in range(self.n)] def root(self, i): inter = set() while self.parent[i]!=i: inter.add(i) i = self.parent[i] r = i for i in inter: self.parent[i] = r return r def merge(self, i, j): # 繋いだかどうかを返す ri = self.root(i) rj = self.root(j) if ri==rj: # self.es[ri] += 1 return False if self.size[ri]>self.size[rj]: ri,rj = rj,ri self.parent[ri] = rj self.size[rj] += self.size[ri] # self.es[rj] += self.es[ri] + 1 return True def rs(self): return sorted(set([self.root(i) for i in range(self.n)])) n,q = list(map(int, input().split())) uf = UF(n) ab = [] for i in range(q): a,b,c = LI() a -= 1 b -= 1 if c==0: uf.merge(a,b) else: ab.append((a,b)) rs = uf.rs() ns = {} M = 998244353 for a,b in ab: ra = uf.root(a) rb = uf.root(b) if ra==rb: ans = 0 break else: ns.setdefault(ra, []).append(rb) ns.setdefault(rb, []).append(ra) num = 0 done = {} for r in rs: if r in done: continue q = [r] done[r] = 0 num += 1 ng = 0 while q: u = q.pop() for v in ns.get(u, []): if v not in done: done[v] = done[u]^1 q.append(v) else: if done[v]==done[u]: ng = 1 break if ng: break if ng: ans = 0 break else: ans = pow(2, num, M) print(ans%M)