#include <bits/stdc++.h>
using namespace std;
const int INF = 1011110000, MOD = 1000000007;
int n, m;
vector<int> bad[101111], gd[101111], G[101111];
int gid[101111], clr[101111];
bool vst[101111];
void dfs(int v, int id) {
  if (gid[v] != -1) return;
  gid[v] = id;
  for (int u : gd[v]) dfs(u, id);
}
bool dfsc(int v, int c) {
  if (clr[v] != -1) {
    return clr[v] == c;
  }
  clr[v] = c;
  for (int u : G[v]) {
    if (dfsc(u, 1 - c) == 0) return 0;
  }
  return 1;
}
void input() {
  cin >> n >> m;
  for (int i = 0, a, b, c; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    if (c) {
      gd[a].push_back(b);
      gd[b].push_back(a);
    } else {
      bad[a].push_back(b);
      bad[b].push_back(a);
    }
  }
}
void solve() {
  memset(gid, -1, sizeof(gid));
  int nc = 0;
  for (int i = 0; i < n; i++) {
    if (gid[i] == -1) {
      dfs(i, nc);
      nc++;
    }
  }
  for (int v = 0; v < n; v++) {
    for (int u : bad[v]) {
      G[gid[v]].push_back(gid[u]);
    }
  }
  int res = 0;
  memset(clr, -1, sizeof(clr));
  for (int i = 0, cnt = 0; i < nc; i++) {
    if (clr[i] == -1) {
      if (!dfsc(i, 0)) {
        printf("0\n");
        return;
      }
      res++;
    }
  }
  long long pres = 1;
  for (int i = 0; i < res - 1; i++) {
    pres = pres * 2 % MOD;
  }
  cout << pres << endl;
}
int main() {
  int t = 1;
  while (t--) {
    input();
    solve();
  }
}
