#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int inf = (1 << 28) - 1;
const int mod = 1000000007;
int n, m;
struct node {
  int next, to, w;
} e[maxn * 2];
int h[maxn], tot = 0;
void add_edge(int a, int b, int c) {
  e[++tot].next = h[a];
  e[tot].to = b;
  e[tot].w = c;
  h[a] = tot;
}
int mark[maxn], ans;
void dfs(int u) {
  for (int i = h[u]; ~i; i = e[i].next) {
    int v = e[i].to;
    int w = e[i].w;
    if (mark[v] == -1) {
      if (w == 1)
        mark[v] = mark[u];
      else
        mark[v] = !mark[u];
      dfs(v);
    } else if (w == 1 && mark[v] != mark[u])
      ans = 0;
    else if (w == 0 && mark[v] == mark[u])
      ans = 0;
  }
}
int main() {
  memset(h, -1, sizeof(h));
  memset(mark, -1, sizeof(mark));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    add_edge(u, v, w);
    add_edge(v, u, w);
  }
  ans = mod / 2 + 1;
  for (int i = 1; i <= n; ++i)
    if (mark[i] == -1) {
      ans = ans * 2 % mod;
      mark[i] = 0;
      dfs(i);
    }
  printf("%d\n", ans);
  return 0;
}
