#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> g[100005];
int visited[100005] = {};
bool flag = 1;
const int mod = 1e9 + 7;
void dfs(int u, int par, int side) {
  visited[u] = side;
  for (auto x : g[u]) {
    int v = x.first, t = x.second;
    if (v == par) {
      continue;
    }
    if (!visited[v]) {
      dfs(v, u, t ? side : 3 - side);
    } else {
      if (t == 1) {
        if (visited[v] != visited[u]) {
          flag = 0;
        }
      } else {
        if (visited[v] == visited[u]) {
          flag = 0;
        }
      }
    }
  }
  return;
}
int modpow(int n) {
  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = (ans * 2) % mod;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v, t;
    cin >> u >> v >> t;
    u--, v--;
    g[u].emplace_back(v, t);
    g[v].emplace_back(u, t);
  }
  int cmp = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i, -1, 1);
      cmp++;
    }
  }
  cout << flag * modpow(cmp - 1) << endl;
  return 0;
}
