#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > edge[100005];
int v[100005];
int t[100005];
bool ok;
void DFS(int x, int y) {
  v[x] = 1;
  for (auto e : edge[x]) {
    int i = e.first;
    if (i == y) continue;
    if (v[i]) {
      int zero = (t[i] ^ t[x]);
      if (zero % 2 == e.second % 2) ok = false;
    } else {
      t[i] = (t[x] + (1 - e.second)) % 2;
      DFS(i, x);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (0); i < (m); i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    edge[x].emplace_back(y, z);
    edge[y].emplace_back(x, z);
  }
  ok = true;
  int com = 0;
  for (int i = (1); i <= (n); i++) {
    if (v[i]) continue;
    DFS(i, 0);
    com++;
  }
  if (!ok)
    puts("0");
  else {
    long long res = 1ll;
    for (int i = (1); i < (com); i++) res = (res * 2) % 1000000007ll;
    printf("%lld\n", res);
  }
  return 0;
}
