#include <bits/stdc++.h>
using namespace std;
vector<int> a[100001], c[100001];
int d[100001], n, m, u, v, k, kq, f[100001];
const int mod = round(1e9) + 7;
void DFS(int u, int p) {
  d[u] = 1;
  for (int i = 0; i < a[u].size(); i++) {
    int v = a[u][i];
    if (d[v] == 0) {
      f[v] = f[u] ^ c[u][i];
      DFS(v, u);
    } else if ((f[v] ^ f[u] ^ c[u][i]) == 1) {
      cout << 0;
      exit(0);
    }
  }
}
int main() {
  cin >> n >> m;
  while (m--) {
    cin >> u >> v >> k;
    k = 1 - k;
    a[u].push_back(v);
    a[v].push_back(u);
    c[u].push_back(k);
    c[v].push_back(k);
  }
  for (u = 1; u <= n; u++)
    if (d[u] == 0) {
      DFS(u, 0);
      if (kq == 0)
        kq = 1;
      else
        kq = kq * 2 % mod;
    }
  cout << kq;
}
