#include <bits/stdc++.h>
using namespace std;
struct Union {
  vector<int> p, r;
  Union(int n) : p(n + 1), r(n + 1, 1) {
    for (int i = 0; i < n; ++i) {
      p[i] = i;
    }
  }
  int find(int u) {
    if (u == p[u]) {
      return u;
    }
    return p[u] = find(p[u]);
  }
  void merge(int u, int v) {
    u = find(u);
    v = find(v);
    if (r[u] > r[v]) {
      swap(u, v);
    }
    p[u] = v;
    if (r[u] == r[v]) {
      ++r[v];
    }
  }
  bool isSmae(int u, int v) { return find(u) == find(v); }
};
const int mod = 1e9 + 7;
int n, m, u, v, w;
int main() {
  scanf("%d%d", &n, &m);
  Union uni(2 * n + 2);
  while (m--) {
    scanf("%d%d%d", &u, &v, &w);
    if (w) {
      if (uni.isSmae(u, v + n)) {
        puts("0");
        return 0;
      }
      uni.merge(u, v);
      uni.merge(u + n, v + n);
    } else {
      if (uni.isSmae(u, v)) {
        puts("0");
        return 0;
      }
      uni.merge(u, v + n);
      uni.merge(u + n, v);
    }
  }
  int cnt = -1;
  for (int i = 1; i <= n; ++i) {
    if (uni.find(i) == i) {
      ++cnt;
    }
  }
  int sol = 1;
  while (cnt--) {
    sol *= 2;
    sol %= mod;
  }
  printf("%d\n", sol);
}
