import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Queue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int q = Integer.parseInt(sa[1]); List> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } Map map = new HashMap<>(); for (int i = 0; i < q; i++) { sa = br.readLine().split(" "); int a = Integer.parseInt(sa[0]) - 1; int b = Integer.parseInt(sa[1]) - 1; int c = Integer.parseInt(sa[2]) + 1; list.get(a).add(b); list.get(b).add(a); long k = (long) a * n + b; map.put(k, c); } br.close(); int mod = 998244353; int[] d = new int[n]; long ans = 1; for (int i = 0; i < n; i++) { if (d[i] != 0) { continue; } d[i] = 1; Queue que = new ArrayDeque<>(); que.add(i); while (!que.isEmpty()) { int cur = que.poll(); for (int next : list.get(cur)) { long k1 = (long) cur * n + next; long k2 = (long) next * n + cur; int vc = d[cur]; int vn = d[next]; int v1 = 0; int v2 = 0; if (map.containsKey(k1)) { v1 = map.get(k1); if (vc == 1) { d[next] = v1; } else { d[next] = 3 - v1; } if (vn != 0) { if (vc == 1 && vn != v1 || vc == 2 && vn == v1) { System.out.println(0); return; } } } if (map.containsKey(k2)) { v2 = map.get(k2); if (vn == 0) { if (d[cur] == 1) { d[next] = v2; } else { d[next] = 3 - v2; } } else { if (vn == 1 && vc != v2 || vn == 2 && vc == v2) { System.out.println(0); return; } } } if (vn == 0) { que.add(next); } } } ans *= 2; ans %= mod; } System.out.println(ans); } }