#include <bits/stdc++.h>
using namespace std;
const int sz = 100005, mod = 1000000007;
int n, m;
vector<int> gd[sz], bd[sz], bad[sz];
int gpid[sz], gp;
int clr[sz];
void dfs(int x) {
  if (gpid[x] != -1) return;
  gpid[x] = gp;
  for (int i : gd[x]) {
    dfs(i);
  }
}
int dfsclr(int x, int col) {
  if (clr[x] != -1) return clr[x] == col;
  clr[x] = col;
  for (int i : bad[x]) {
    if (dfsclr(i, 1 - col) == 0) return 0;
  }
  return 1;
}
void sol() {
  int cnt = 0;
  for (int i = 0; i < gp; i++) {
    if (clr[i] == -1) {
      if (!dfsclr(i, 1)) {
        printf("0\n");
        return;
      }
      cnt++;
    }
  }
  long long ans = 1;
  cnt--;
  while (cnt) {
    ans *= 2;
    ans %= mod;
    cnt--;
  }
  printf("%lld\n", ans);
}
void hi(int x) {
  x++;
  return;
}
int main() {
  int i, a, b, c;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (i = 0; i < m; i++) {
      scanf("%d %d %d", &a, &b, &c);
      if (c != 1) {
        bd[a].push_back(b);
        bd[b].push_back(a);
      } else {
        gd[a].push_back(b);
        gd[b].push_back(a);
      }
    }
    memset(gpid, -1, sizeof(gpid));
    gp = 0;
    for (i = 1; i <= n; i++) {
      hi(9);
      if (gpid[i] == -1) {
        dfs(i);
        gp++;
      }
    }
    for (i = 1; i <= n; i++) {
      for (int j : bd[i]) {
        bad[gpid[i]].push_back(gpid[j]);
      }
    }
    memset(clr, -1, sizeof(clr));
    sol();
  }
  return 0;
}
