#include <bits/stdc++.h>
using namespace std;
int getint() {
  int s = 0, o = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-') o = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) s *= 10, s += c - '0';
  return s * o;
}
const long long MOD = (long long)1e9 + 7;
const unsigned long long inf = (long long)1e18;
const int maxn = 100001;
int f[maxn], F[maxn];
int find(int x) {
  if (f[x] == x) {
    return x;
  } else {
    find(f[x]);
    F[x] ^= F[f[x]];
    f[x] = f[f[x]];
    return f[x];
  }
}
int main(int argc, char const *argv[]) {
  int n, k, x, y, z;
  cin >> n >> k;
  for (int i = 1; i < 1 + n; i++) f[i] = i;
  bool bad = 0;
  for (int kk = 0; kk < k; kk++) {
    cin >> x >> y >> z;
    z ^= 1;
    int u = find(x), v = find(y);
    if (u == v) {
      if (F[x] ^ F[y] ^ z) {
        bad = 1;
        break;
      }
    } else {
      f[v] = u;
      F[v] = z ^ F[x] ^ F[y];
    }
  }
  if (bad)
    printf("0\n");
  else {
    bool ft = 1;
    long long ans = 1;
    for (int i = 1; i < 1 + n; i++) {
      if (find(i) == i) {
        if (ft)
          ft = 0;
        else
          ans = (ans * 2) % MOD;
      }
    }
    cout << ans;
  }
  return 0;
}
