#include <bits/stdc++.h>
using namespace std;
const long long N = 200100;
const long long M = 1000000007LL;
int n, m;
long long fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) fa[fx] = fy;
}
void init() {
  for (int i = 1; i < N; i++) fa[i] = i;
}
int main() {
  ios::sync_with_stdio(0);
  int a, b, c;
  cin >> n >> m;
  init();
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    if (c) {
      if (find(a + n) == find(b)) {
        puts("0");
        return 0;
      }
      merge(a, b);
      merge(a + n, b + n);
    } else {
      if (find(a) == find(b)) {
        puts("0");
        return 0;
      }
      merge(a + n, b);
      merge(a, b + n);
    }
  }
  int block = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i) block++;
  }
  long long ans = 1;
  for (int i = 0; i < block - 1; i++) {
    ans <<= 1;
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}
