#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
vector<int> f;
vector<vector<pair<int, int> > > g;
bool ok = true;
void dfs(int v) {
  for (vector<pair<int, int> >::iterator it = g[v].begin(); it != g[v].end();
       ++it) {
    int to = it->first, c = it->second;
    if (f[to] == -1) {
      f[to] = f[v] ^ c;
      dfs(to);
    }
    ok &= f[to] == (f[v] ^ c) ? true : false;
  }
}
int main() {
  int n, m, cnt = 0;
  cin >> n >> m;
  g.resize(n);
  f.resize(n);
  for (int i = 0; i < m; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    g[x - 1].push_back(make_pair(y - 1, 1 - c));
    g[y - 1].push_back(make_pair(x - 1, 1 - c));
  }
  for (int i = 0; i < n; ++i) {
    f[i] = -1;
  }
  for (int i = 0; i < n; ++i) {
    if (f[i] == -1) {
      f[i] = 0;
      dfs(i);
      ++cnt;
    }
  }
  if (!ok) {
    cout << 0 << endl;
  } else {
    int ans = 1;
    for (int i = 0; i < cnt - 1; ++i) {
      ans = (2 * ans) % MOD;
    }
    cout << ans << endl;
  }
  return 0;
}
