#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
int fa[maxn], d[maxn];
void init_set(int n) {
  for (int i = 1; i <= n; i++) fa[i] = i, d[i] = 0;
}
int find_set(int rt) {
  if (rt != fa[rt]) {
    int root = find_set(fa[rt]);
    d[rt] = (d[rt] + d[fa[rt]] + 2) % 2;
    return fa[rt] = root;
  }
  return rt;
}
bool union_set(int u, int v, int c) {
  int fu = find_set(u), fv = find_set(v);
  if (fu == fv) {
    if ((d[v] + d[u]) % 2 != c) return false;
    return true;
  }
  fa[fu] = fv;
  d[fu] = (d[v] + c - d[u] + 2) % 2;
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  init_set(n);
  bool ok = true;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    ok &= union_set(u, v, 1 - c);
  }
  if (!ok) {
    puts("0");
    return 0;
  }
  int cnt = 0, ans = 1;
  for (int i = 1; i <= n; i++)
    if (i == find_set(i)) ++cnt;
  for (int i = 0; i < cnt - 1; i++) ans = ans * 2 % mod;
  cout << ans << endl;
}
