#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
int add(int a, int b) {
  if (a + b < MOD) {
    return a + b;
  }
  return a + b - MOD;
}
int sub(int a, int b) {
  if (a >= b) {
    return a - b;
  }
  return a - b + MOD;
}
int mul(int a, int b) { return 1LL * a * b % MOD; }
int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}
int divv(int a, int b) { return mul(a, binpow(b, MOD - 2)); }
vector<int> g[N];
vector<int> ng[N];
int used[N];
int comp[N];
int color[N];
int bad = 0;
void dfs(int cur, int c) {
  used[cur] = 1;
  comp[cur] = c;
  for (auto t : g[cur]) {
    if (!used[t]) {
      dfs(t, c);
    }
  }
}
void dfs2(int cur, int clr) {
  used[cur] = 1;
  color[cur] = clr;
  for (auto t : ng[cur]) {
    if (!used[t]) {
      dfs2(t, clr ^ 1);
    }
    bad |= (color[cur] == color[t]);
  }
}
vector<pair<int, int>> be;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    if (c) {
      g[u].push_back(v);
      g[v].push_back(u);
    } else {
      be.push_back({u, v});
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      dfs(i, cnt);
      cnt++;
    }
  }
  {
    int ok = 1;
    for (auto [u, v] : be) {
      ok &= (comp[u] != comp[v]);
    }
    if (!ok) {
      cout << 0 << '\n';
      return 0;
    }
  }
  int ans = 1;
  {
    for (auto [u, v] : be) {
      ng[comp[u]].push_back(comp[v]);
      ng[comp[v]].push_back(comp[u]);
    }
    fill(used, used + cnt, 0);
    for (int i = 0; i < cnt; i++) {
      if (!used[i]) {
        dfs2(i, 0);
        ans = mul(ans, 2);
      }
    }
    ans = divv(ans, 2);
  }
  if (bad) {
    ans = 0;
  }
  cout << ans << '\n';
}
