#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long MOD = 1e9 + 7;
long long n, m, connected = 0;
long long col[N], vis[N];
vector<pair<long long, long long> > g[N];
long long pow(long long a, long long b, long long m) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
void dfs(long long k, long long c) {
  if (vis[k]) {
    if (col[k] != c) {
      cout << "0";
      exit(0);
    }
    return;
  }
  vis[k] = 1;
  col[k] = c;
  for (auto &it : g[k]) {
    dfs(it.first, c ^ (1 - it.second));
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long u, v, t;
    cin >> u >> v >> t;
    g[u].push_back({v, t});
    g[v].push_back({u, t});
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, 0);
      connected++;
    }
  }
  long long ans = pow(2LL, connected - 1, MOD);
  cout << ans;
  return 0;
}
