#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int fa[N], rk[N];
int root(int a) {
  if (fa[a] == a)
    return a;
  else {
    int ret;
    ret = root(fa[a]);
    rk[a] ^= rk[fa[a]];
    return fa[a] = ret;
  }
}
void mg(int a, int b, int c) {
  int pa = root(a);
  int pb = root(b);
  fa[pa] = pb;
  if (c)
    rk[pa] = (rk[a] + rk[b]) % 2;
  else
    rk[pa] = (rk[a] + rk[b] + 1) % 2;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int i;
  for (i = 1; i <= n; i++) {
    fa[i] = i;
    rk[i] = 0;
  }
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (c && root(a) == root(b) && rk[a] != rk[b]) {
      puts("0");
      return 0;
    }
    if (!c && root(a) == root(b) && rk[a] == rk[b]) {
      puts("0");
      return 0;
    }
    mg(a, b, c);
  }
  int cnt = -1;
  for (i = 1; i <= n; i++) {
    if (fa[i] == i) cnt++;
  }
  int ans = 1;
  while (cnt--) {
    ans <<= 1;
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}
