#include <bits/stdc++.h>
using namespace std;
const int N = 210000;
const long long oo = 1000000007L;
struct Node {
  int y, x, next;
} E[900100];
bool vis[N];
int col[N];
int top;
int head[N];
int a[N], b[N], c[N];
void init() {
  top = 0;
  memset(head, -1, sizeof(head));
  memset(col, 0, sizeof(col));
}
void addEdge(int u, int v, int x) {
  E[++top].y = v;
  E[top].x = x;
  E[top].next = head[u];
  head[u] = top;
}
bool dfs(int x, int color) {
  bool an = true;
  col[x] = color;
  for (int i = head[x]; i != -1; i = E[i].next) {
    int y = E[i].y;
    if (E[i].x == 0) {
      if (col[y] != 0 && col[y] == col[x]) return false;
      if (col[y] == 0) {
        an = dfs(y, -color);
        if (!an) return false;
      }
    } else {
      if (col[y] != 0 && col[y] != col[x]) return false;
      if (col[y] == 0) {
        an = dfs(y, color);
        if (!an) return false;
      }
    }
  }
  return an;
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    bool flag = 1;
    init();
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &a[i], &b[i], &c[i]);
      addEdge(a[i], b[i], c[i]);
      addEdge(b[i], a[i], c[i]);
    }
    int tot = 0;
    for (int i = 1; i <= n; i++) {
      if (col[i] == 0) {
        tot++;
        flag = dfs(i, tot);
        if (!flag) break;
      }
    }
    if (!flag)
      printf("0\n");
    else {
      long long ans = 1;
      for (int i = 1; i < tot; i++) ans = ans * 2L % oo;
      printf("%lld\n", ans);
    }
  }
  return 0;
}
