#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int p[111111], t[111111];
bool k = true;
int find(int v) {
  if (p[v] == v) {
    return v;
  }
  int pr = p[v];
  p[v] = find(p[v]);
  t[v] ^= t[pr];
  return p[v];
}
void un(int v1, int v2, int t0) {
  int p1, p2;
  p1 = find(v1);
  p2 = find(v2);
  if (p1 == p2) {
    if (t[v1] ^ t[v2] ^ t0 == 1) {
      k = false;
    }
  } else {
    p[p2] = p1;
    t[p2] = t[v1] ^ t[v2] ^ t0;
  }
}
void solve(int n, int m) {
  int i, j, v1, v2, a, b, c, ans;
  for (i = 0; i < n; i++) {
    p[i] = i;
    t[i] = 0;
  }
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    un(a - 1, b - 1, 1 ^ c);
  }
  for (i = 0; i < n; i++) {
    find(i);
  }
  if (!k) {
    printf("0");
    return;
  }
  ans = 5e8 + 4;
  for (i = 0; i < n; i++) {
    if (p[i] == i) {
      ans *= 2;
      ans %= mod;
    }
  }
  printf("%d", ans);
}
int main() {
#pragma comment(linker, "/STACK:268435456")
#pragma warning(disable : 4996)
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    solve(n, m);
  }
  return 0;
}
