def solve(): N,Q=map(int,input().split()) Data=[[] for _ in range(N+1)] for i in range(Q): A,B,C=map(int,input().split()) Data[A].append((B,C)) Data[B].append((A,C)) T=[0]*(N+1) ans=1; Mod=998244353 for x in range(1,N+1): if T[x]: continue E=[set([x]), set()] S=[x]; T[x]=1 while S: y=S.pop() mode=0 if y in E[0] else 1 for b,c in Data[y]: if (b in E[0]) or (b in E[1]): if b not in E[mode^c]: return 0 else: S.append(b) E[mode^c].add(b) T[b]=1 ans*=2 ans%=Mod return ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write print(solve())