#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, mod = 1e9 + 7;
int n, m, dad[maxn], mark[maxn];
bool used[maxn];
vector<int> way[maxn];
inline int findset(int x) {
  if (x == dad[x]) return x;
  return dad[x] = findset(dad[x]);
}
void dfs(int node, int dad, int cnt) {
  mark[node] = cnt;
  for (int i = 0; i < (int)way[node].size(); i++) {
    int adj = way[node][i];
    if (adj == dad) continue;
    if (mark[adj]) {
      if ((mark[node] & 1) == (mark[adj] & 1)) {
        puts("0");
        exit(0);
      }
    } else
      dfs(adj, node, cnt + 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) dad[i] = i;
  vector<pair<int, int> > edge;
  for (int i = 0, a, b, c; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    if (c) {
      a = findset(a), b = findset(b);
      if (a != b) {
        if (rand() & 1) swap(a, b);
        dad[a] = b;
      }
    } else {
      edge.push_back(pair<int, int>(a, b));
    }
  }
  for (int i = 0; i < (int)edge.size(); i++) {
    int a = findset(edge[i].first), b = findset(edge[i].second);
    if (a == b) {
      puts("0");
      return 0;
    }
    way[a].push_back(b);
    way[b].push_back(a);
  }
  int cnt = -1;
  for (int i = 1; i <= n; i++) {
    int x = findset(i);
    if (!mark[x]) {
      cnt++;
      dfs(x, 0, 1);
    }
  }
  int a = 2, ans = 1;
  while (cnt) {
    if (cnt & 1) ans = 1ll * ans * a % mod;
    a = 1ll * a * a % mod;
    cnt >>= 1;
  }
  printf("%d\n", ans);
  return 0;
}
