#include <bits/stdc++.h>
using namespace std;
int color[100005];
bool vis[100005];
vector<pair<int, int> > graph[100005];
bool dfs(int i, int colo) {
  if (vis[i]) return color[i] == colo;
  vis[i] = true;
  color[i] = colo;
  for (typeof(graph[i].begin()) it = graph[i].begin(); it != graph[i].end();
       it++) {
    int v = it->first;
    if (!dfs(v, it->second == 1 ? colo : 1 - colo)) return false;
  }
  return true;
}
int main() {
  int n, m, i, j, k;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 1; i < n + 1; i++) vis[i] = 0;
  for (i = 1; i < m + 1; i++) {
    int x, y, c;
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &c);
    graph[x].push_back(make_pair(y, c));
    graph[y].push_back(make_pair(x, c));
  }
  int count = 0;
  for (i = 1; i < n + 1; i++) {
    if (!vis[i]) {
      if (!dfs(i, 0)) {
        printf("0\n");
        return 0;
      }
      count++;
    }
  }
  long long res = 1;
  for (i = 1; i < count; i++) {
    res *= 2LL;
    res %= 1000000007;
  }
  printf("%I64d\n", res);
}
