#include <bits/stdc++.h>
using namespace std;
const int N = 600050;
const long long Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int n, m, pnt[N], nxt[N], qu[N], head[N], cnt, ans, vis[N];
void addedge(int u, int v, int w) {
  pnt[cnt] = v;
  nxt[cnt] = head[u];
  qu[cnt] = w;
  head[u] = cnt++;
}
void dfs(int u) {
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = pnt[i], t = qu[i];
    if (vis[v] == -1) {
      if (t == 1)
        vis[v] = vis[u];
      else
        vis[v] = 1 - vis[u];
      dfs(v);
    }
    if (t == 1) {
      if (vis[v] != vis[u]) ans = 0;
    }
    if (t == 0) {
      if (vis[v] == vis[u]) ans = 0;
    }
  }
}
int main() {
  memset(head, -1, sizeof head), cnt = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < ((m) + 1); i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    addedge(u, v, w);
    addedge(v, u, w);
  }
  ans = (Mod + 1) >> 1;
  memset(vis, -1, sizeof vis);
  for (int i = 1; i < ((n) + 1); i++) {
    if (vis[i] == -1) {
      ans = (ans << 1) % Mod;
      vis[i] = 0;
      dfs(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
