#include <bits/stdc++.h>
int f[100005], kind[100005];
int find(int x) {
  if (f[x] == x) return x;
  int nowfa = f[x];
  f[x] = find(f[x]);
  kind[x] = (kind[x] + kind[nowfa] + 1) % 2;
  return f[x];
}
void unions(int fa1, int fa2, int u, int v, int kinds) {
  f[fa1] = fa2;
  kind[fa1] = (kind[v] - kind[u] + 2 + kinds) % 2;
}
long long quick_pow(long long x, long long y, int mod) {
  long long z = 1;
  while (y) {
    if (y % 2) z = (z * x) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return z;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    kind[i] = 1;
  }
  int u, v, kinds;
  int ok = 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &kinds);
    if (ok == 0) continue;
    int fa1 = find(u), fa2 = find(v);
    if (fa1 == fa2) {
      if (kinds == 0 && kind[u] == kind[v])
        ok = 0;
      else if (kinds == 1 && kind[u] != kind[v])
        ok = 0;
    } else {
      unions(fa1, fa2, u, v, kinds);
    }
  }
  if (ok == 0)
    printf("0\n");
  else {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (f[i] == i) cnt++;
    }
    long long ans = quick_pow(2, cnt - 1, 1000000007);
    printf("%I64d\n", ans);
  }
  return 0;
}
