#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, m, a, b, c, f[MAXN];
vector<pair<int, int> > hate, love;
vector<int> gh[MAXN];
int rep(int v) {
  if (f[v] == v) return v;
  return f[v] = rep(f[v]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    if (c == 1) {
      love.push_back(make_pair(a, b));
      if (rep(a) != rep(b)) f[rep(a)] = rep(b);
    } else {
      hate.push_back(make_pair(a, b));
    }
  }
  for (int i = 0; i < hate.size(); i++) {
    a = rep(hate[i].first);
    b = rep(hate[i].second);
    gh[a].push_back(b);
    gh[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)gh[i].size() - 1; j++) {
      a = gh[i][j];
      b = gh[i][j + 1];
      if (rep(a) != rep(b)) f[rep(a)] = rep(b);
    }
  }
  for (int i = 0; i < hate.size(); i++) {
    a = hate[i].first;
    b = hate[i].second;
    if (rep(a) == rep(b)) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < hate.size(); i++) {
    a = hate[i].first;
    b = hate[i].second;
    f[rep(a)] = rep(b);
  }
  set<int> roz;
  for (int i = 1; i <= n; i++) roz.insert(rep(i));
  int res = 1;
  for (int i = 1; i < roz.size(); i++) res = (res * 2) % 1000000007;
  cout << res << endl;
}
