#include <bits/stdc++.h>
using namespace std;
int fa[100005];
int get(int v) {
  if (fa[v] != v) fa[v] = get(fa[v]);
  return fa[v];
}
vector<int> edge[100005];
int s[100005][2], p, n, mark[100005], flag;
void dfs(int x) {
  if (!flag) return;
  for (int i = 0; i < edge[x].size(); i++) {
    int y = edge[x][i];
    if (!mark[y]) {
      mark[y] = 3 - mark[x];
      dfs(y);
    } else if (mark[y] == mark[x]) {
      flag = 0;
      return;
    }
  }
}
int solve() {
  int i;
  for (i = 0; i < p; i++) {
    int x = get(s[i][0]);
    int y = get(s[i][1]);
    if (x == y) {
      return 0;
    }
    edge[x].push_back(y);
    edge[y].push_back(x);
  }
  for (i = 1; i <= n; i++) mark[i] = 0;
  int cnt = 0;
  for (i = 1; i <= n; i++) {
    int x = get(i);
    if (x == i && !mark[x]) {
      flag = 1;
      mark[x] = 1;
      dfs(x);
      if (flag == 0) return 0;
      cnt++;
    }
  }
  int res = 1;
  for (i = 1; i < cnt; i++) res = (res * 2) % 1000000007;
  return res;
}
int main() {
  int m, i, j, k, a, b;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (i = 1; i <= n; i++) fa[i] = i;
    p = 0;
    for (i = 0; i < m; i++) {
      scanf("%d %d %d", &a, &b, &k);
      if (k == 0) {
        s[p][0] = a, s[p++][1] = b;
      } else {
        int x = get(a);
        int y = get(b);
        fa[x] = y;
      }
    }
    printf("%d\n", solve());
  }
  return 0;
}
