#include <bits/stdc++.h>
using namespace std;
long long int inline ipow(long long int a, long long int b, long long int m) {
  long long int val = 1;
  a %= m;
  while (b) {
    if (b & 01) val = (val * a) % m;
    b >>= 1;
    a = (a * a) % m;
  };
  return val % m;
}
long long int inline ipow(long long int a, long long int b) {
  long long int val = 1;
  while (b) {
    if (b & 01) val = (val * a);
    b >>= 1;
    a = (a * a);
  };
  return val;
}
int parent[100005], type[100005];
int find(int x) {
  if (parent[x] == x) return x;
  int p = parent[x];
  parent[x] = find(parent[x]);
  type[x] = (type[x] + type[p] + 1) % 2;
  return parent[x];
}
void uni(int x, int y, int u, int v, int typ) {
  parent[x] = y;
  type[x] = (type[v] - type[u] + typ) % 2;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t tStart = clock();
  int i, x, y, px, py, typ, n, m;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    parent[i] = i;
    type[i] = 1;
  }
  bool fl = true;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> typ;
    if (!fl) continue;
    px = find(x);
    py = find(y);
    if (px == py) {
      if (typ == 0 && type[x] == type[y])
        fl = false;
      else if (typ == 1 && type[x] != type[y])
        fl = false;
    } else
      uni(px, py, x, y, typ);
  }
  if (!fl)
    cout << "0\n";
  else {
    int num = 0;
    for (i = 1; i <= n; i++)
      if (parent[i] == i) num++;
    cout << ipow(2, num - 1, 1000000007);
  }
  return 0;
}
