N,Q=map(int,input().split()) E=[[] for i in range(N+1)] for i in range(Q): x,y,c=map(int,input().split()) E[x].append((y,c)) E[y].append((x,c)) ANS=[-1]*(N+1) mod=998244353 A=0 for i in range(1,N+1): if ANS[i]==-1: A+=1 ANS[i]=0 Q=[i] while Q: x=Q.pop() for to,c in E[x]: if c==0: if ANS[to]==-1: Q.append(to) ANS[to]=ANS[x] else: if ANS[to]!=ANS[x]: print(0) exit() else: if ANS[to]==-1: Q.append(to) ANS[to]=ANS[x]^1 else: if ANS[to]==ANS[x]: print(0) exit() print(pow(2,A,mod))