#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
int n, m, mark[maxn], color[maxn], col = 0, ted = 0, ans;
vector<pair<int, int> > hate;
vector<int> G[maxn], Gp[maxn], Gz[maxn];
long long tav(int b) {
  if (b < 0) return 0;
  long long ans = 1ll;
  for (int i = 0; i < b; i++) {
    ans *= 2ll;
    ans %= 1ll * mod;
  }
  return ans;
}
void dfs(int u) {
  color[u] = col;
  for (int i = 0; i < G[u].size(); i++)
    if (color[G[u][i]] == 0) dfs(G[u][i]);
  return;
}
bool dfsp(int u, int par) {
  mark[u] = ted + par;
  for (int i = 0; i < Gp[u].size(); i++) {
    if (!mark[Gp[u][i]]) {
      if (dfsp(Gp[u][i], 1 - par)) return 1;
    } else if (mark[Gp[u][i]] != ted + 1 - par)
      return 1;
  }
  return 0;
}
void prep(int u) {
  sort(Gz[u].begin(), Gz[u].end());
  int i = 0;
  while (i < Gz[u].size()) {
    Gp[u].push_back(Gz[u][i]);
    while (i + 1 < Gz[u].size() && Gz[u][i + 1] == Gz[u][i]) i++;
    i++;
  }
}
bool bad() {
  for (int i = 0; i < hate.size(); i++) {
    int u = hate[i].first, v = hate[i].second;
    if (color[u] == color[v]) return 1;
    Gz[color[u]].push_back(color[v]);
    Gz[color[v]].push_back(color[u]);
  }
  for (int i = 1; i <= col; i++) prep(i);
  for (int i = 1; i <= col; i++) {
    if (!mark[i]) {
      ted += 2;
      ans++;
      if (dfsp(i, 0)) {
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    if (w) {
      G[u].push_back(v), G[v].push_back(u);
    } else {
      hate.push_back(pair<int, int>(u, v));
    }
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == 0) {
      col++;
      dfs(i);
    }
  }
  if (bad()) {
    cout << 0;
    return 0;
  }
  cout << tav(ans - 1);
}
