#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
struct edge {
  int v, w, next;
} e[100005 * 4];
int tot = 0;
int head[100005];
void addedge(int u, int v, int w) {
  e[tot].v = v;
  e[tot].w = w;
  e[tot].next = head[u];
  head[u] = tot++;
}
int color[100005];
int ans = 0;
void dfs_color(int u) {
  for (int i = head[u]; i != -1; i = e[i].next) {
    int v = e[i].v;
    int w = e[i].w;
    if (color[v] == -1) {
      if (w)
        color[v] = color[u];
      else
        color[v] = !color[u];
      dfs_color(v);
    } else {
      if (w && color[v] != color[u]) ans = 0;
      if (w == 0 && color[v] == color[u]) ans = 0;
    }
  }
}
void init() {
  tot = 0;
  memset((head), (-1), sizeof((head)));
  memset((color), (-1), sizeof((color)));
}
int main() {
  int n, m, u, v, w;
  init();
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> w;
    addedge(u, v, w);
    addedge(v, u, w);
  }
  ans = (1000000007 + 1) / 2;
  for (int i = 1; i <= n; i++) {
    if (color[i] == -1) {
      color[i] = 0;
      dfs_color(i);
      ans = (ans + ans) % 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
