#include <bits/stdc++.h>
using namespace std;
const int md = 1000000007;
int n, m;
int vis[100010], col[100010];
vector<pair<int, int> > g[100010];
void dfs(int v, int u, int c) {
  if (vis[v]) {
    if (col[v] != c) {
      cout << 0 << endl;
      exit(0);
    }
    return;
  }
  vis[v] = 1;
  col[v] = c;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first, f = g[v][i].second;
    if (to != u) dfs(to, v, (f ? c : -c));
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    g[x].push_back(make_pair(y, z));
    g[y].push_back(make_pair(x, z));
  }
  int k = 0;
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, -1, 1);
      k++;
    }
  }
  for (int i = 0; i < k - 1; i++) {
    ans <<= 1;
    ans %= md;
  }
  cout << ans << endl;
  return 0;
}
