#include <bits/stdc++.h>
using namespace std;
void read(int &num) {
  char c = getchar();
  int sgn = 1;
  num = 0;
  while (c != EOF && (c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') sgn = -1, c = getchar();
  while (c != EOF && c >= '0' && c <= '9')
    num = num * 10 + c - '0', c = getchar();
  num *= sgn;
}
const int N = 200005;
const int M = 1000000007;
int n, m, f[N], x, y, z, X, Y, c[N];
bool bo;
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  read(n), read(m);
  for (int i = 1; i <= n * 2; i++) f[i] = i;
  bo = true;
  for (int i = 1; i <= m; i++) {
    read(x), read(y), read(z);
    X = x + n;
    Y = y + n;
    x = find(x);
    y = find(y);
    X = find(X);
    Y = find(Y);
    if (z == 0 && x == y || z == 1 && x == Y) {
      bo = false;
      break;
    }
    if (z == 0)
      f[x] = Y, f[y] = X;
    else
      f[x] = y, f[X] = Y;
  }
  if (bo) {
    for (int i = 1; i <= n; i++) {
      x = find(i);
      y = find(i + n);
      if (x != y) f[x] = y;
    }
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= n; i++) {
      int t = find(i);
      if (t > n) t -= n;
      c[t]++;
    }
    int tot = 0, ans = 1;
    for (int i = 1; i <= n; i++)
      if (c[i]) tot++;
    for (int i = 1; i <= tot - 1; i++) ans = (ans * 2) % M;
    printf("%d\n", ans);
  } else
    puts("0");
  return 0;
}
