#include <bits/stdc++.h>
using namespace std;
int viz[100005], viz2[100005];
vector<pair<int, int> > v[100005];
void dfs(int node) {
  viz2[node] = 1;
  for (int i = 0; i < v[node].size(); ++i) {
    if (viz2[v[node][i].first] == 0) dfs(v[node][i].first);
  }
}
bool ok = true;
void check(int node, int mark) {
  viz[node] = mark;
  for (int i = 0; i < v[node].size(); ++i) {
    if (viz[v[node][i].first] == 0)
      if (v[node][i].second == 0)
        check(v[node][i].first, -mark);
      else
        check(v[node][i].first, mark);
    else {
      if (viz[v[node][i].first] != -mark && v[node][i].second == 0) ok = false;
      if (viz[v[node][i].first] != mark && v[node][i].second == 1) ok = false;
    }
  }
}
int main() {
  int n, m, a, b, c;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    --a;
    --b;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  for (int i = 0; i < n; ++i) {
    if (viz[i] == 0) check(i, 1);
  }
  int nrComp = 0;
  for (int i = 0; i < n; ++i) {
    if (viz2[i] == 0) {
      dfs(i);
      ++nrComp;
    }
  }
  if (ok == false)
    cout << 0;
  else {
    long long ans = 1;
    for (int i = 0; i < nrComp - 1; ++i) ans = (ans * 2) % 1000000007;
    cout << ans;
  }
}
