#include <bits/stdc++.h>
using namespace std;
long long int modulo = 1e9 + 7;
const int limite = 1000000;
int n, m;
vector<pair<int, int> > g[limite];
int visto[limite];
void genera(int u, int l) {
  if (visto[u]) {
    if (visto[u] != l) {
      cout << 0 << endl;
      exit(0);
    }
    return;
  }
  visto[u] = l;
  vector<pair<int, int> > &ar = g[u];
  for (int i = 0; i < int(ar.size()); i++) {
    int v = ar[i].first;
    int c = ar[i].second;
    if (c == 1)
      genera(v, l);
    else
      genera(v, 3 - l);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    g[u].push_back(pair<int, int>(v, c));
    g[v].push_back(pair<int, int>(u, c));
  }
  int c = 0;
  for (int i = 1; i <= n; i++) {
    if (not visto[i]) {
      c++;
      genera(i, 1);
    }
  }
  long long int sol = 1;
  for (int i = 0; i < c - 1; i++) sol = 2 * sol % modulo;
  cout << sol << endl;
}
