#include <bits/stdc++.h>
using namespace std;
long long Pow(int x) {
  long long ans = 1, a = 2;
  while (x) {
    if (x & 1) ans = (ans * a) % 1000000007;
    a = (a * a) % 1000000007;
    x >>= 1;
  }
  return ans;
}
int fa[100005];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void Union(int x, int y) {
  if (find(x) != find(y)) fa[fa[x]] = fa[y];
}
struct node {
  int next;
  int to;
  bool cost;
} E[100005 << 1];
int head[100005], nn;
void add(int a, int b, bool c) {
  E[nn].to = b;
  E[nn].next = head[a];
  E[nn].cost = c;
  head[a] = nn++;
}
int id[100005];
bool vis[100005], xorsum[100005], succ = true;
void dfs(int u, int fu, int idu, bool xsum) {
  if (!succ) return;
  vis[u] = true;
  id[u] = idu;
  xorsum[u] = xsum;
  for (int i = head[u]; ~i; i = E[i].next) {
    int v = E[i].to;
    bool c = E[i].cost;
    if (v == fu) continue;
    if (!vis[v])
      dfs(v, u, idu + 1, xsum ^ c);
    else {
      bool num = (idu + 1 - id[v]) & 1;
      bool sum = xsum ^ c ^ xorsum[v];
      if (num != sum) {
        succ = false;
        return;
      }
    }
  }
}
bool judge(int n) {
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i, 0, 1, 0);
    if (!succ) return false;
  }
  return true;
}
void solve(int n) {
  if (judge(n)) {
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (fa[i] == i) cnt++;
    printf("%I64d\n", Pow(cnt - 1));
  } else
    printf("0\n");
}
void init(int n) {
  for (int i = 1; i <= n; i++) fa[i] = i;
  memset(head, -1, sizeof(head));
  nn = 1;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  init(n);
  int a, b, c;
  while (m--) {
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c);
    add(b, a, c);
    Union(a, b);
  }
  solve(n);
  return 0;
}
