#include <bits/stdc++.h>
using namespace std;
const int nn = 1e5 + 5;
const int mod = 1000000007;
int father[nn], n, m, tot, a[nn], b[nn], c[nn];
pair<int, int> hate[nn];
int vis[nn];
int getfather(int p) {
  return father[p] == p ? p : father[p] = getfather(father[p]);
}
inline void merge(int p, int q) { father[getfather(p)] = getfather(q); }
int cnt, Begin[nn << 1], End[nn << 1], Last[nn], Prev[nn << 1];
inline void addedge(int p, int q) {
  cnt++;
  Begin[cnt] = p;
  End[cnt] = q;
  Prev[cnt] = Last[p];
  Last[p] = cnt;
}
void dfs(int p, int ff) {
  vis[p] = ff;
  for (int i = Last[p]; i; i = Prev[i]) {
    if (vis[End[i]] == 1) {
      dfs(End[i], ~ff);
    } else {
      if (~ff != vis[End[i]]) {
        printf("0");
        exit(0);
      }
    }
  }
}
int ksm(int x, int n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * x) % mod;
    n >>= 1;
    x = 1ll * x * x % mod;
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    if (c[i] == 1)
      merge(a[i], b[i]);
    else
      hate[++tot] = pair<int, int>{a[i], b[i]};
  }
  for (int i = 1; i <= tot; i++) {
    hate[i].first = getfather(hate[i].first);
    hate[i].second = getfather(hate[i].second);
    addedge(hate[i].first, hate[i].second);
    addedge(hate[i].second, hate[i].first);
  }
  for (int i = 1; i <= n; i++) vis[i] = 1;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (vis[getfather(i)] == 1) {
      ans++;
      dfs(getfather(i), 0);
    }
  printf("%d", ksm(2, ans - 1));
  return 0;
}
