#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
vector<vector<pair<int, int> > > g;
vector<int> tin, group;
int timer;
void dfs(int u) {
  tin[u] = timer;
  for (auto& v : g[u]) {
    if (tin[v.first]) {
      if (tin[v.first] == timer && group[v.first] != (group[u] ^ v.second)) {
        printf("0");
        exit(0);
      }
      continue;
    }
    group[v.first] = (group[u] ^ v.second);
    dfs(v.first);
  }
}
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  g.resize(N + 1);
  for (int i = 1, a, b, c; i <= M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    g[a].push_back({b, !c});
    g[b].push_back({a, !c});
  }
  tin = group = vector<int>(N + 1, 0);
  int com = 0;
  for (int i = 1; i <= N; i++) {
    if (!tin[i]) {
      timer++;
      com++;
      dfs(i);
    }
  }
  long long ans = 1;
  for (int i = 1; i <= com - 1; i++) {
    ans = ans * 2LL % mod;
  }
  printf("%lld", ans);
  return 0;
}
