#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 100005;
vector<pair<int, int> > mat[MAXN];
int m, n;
int vis[MAXN];
long long ans;
void dfs(int u) {
  for (int i = 0; i < mat[u].size(); i++) {
    int v = mat[u][i].first;
    int p = mat[u][i].second;
    if (vis[v] == -1) {
      if (p == 1)
        vis[v] = vis[u];
      else
        vis[v] = !vis[u];
      dfs(v);
    }
    if (p == 1 && vis[u] != vis[v]) ans = 0;
    if (p == 0 && vis[u] == vis[v]) ans = 0;
  }
}
int main() {
  int i, j, k, x, y, z;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y >> z;
    mat[x].push_back(make_pair(y, z));
    mat[y].push_back(make_pair(x, z));
  }
  memset(vis, -1, sizeof(vis));
  ans = (mod + 1) / 2;
  for (i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      ans = (ans * 2) % mod;
      vis[i] = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
  return 0;
}
