#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int N = 1e5;
vector<pair<int, int> > g[N];
bool col[N], used[N];
bool dfs(int v, int c) {
  used[v] = true;
  col[v] = c;
  for (pair<int, int>& to : g[v]) {
    int u = to.first;
    if (!used[u]) {
      if (!dfs(u, c ^ to.second)) return false;
    } else if (col[u] ^ col[v] != to.second)
      return false;
  }
  return true;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0, u, v, type; i < m; ++i) {
    scanf("%d%d%d", &u, &v, &type);
    --u;
    --v;
    type = 1 - type;
    g[u].push_back({v, type});
    g[v].push_back({u, type});
  }
  long long ans = 1;
  int cnt = 0;
  for (int v = 0; v < n; ++v) {
    if (!used[v]) {
      if (!dfs(v, 0)) {
        printf("0");
        return 0;
      }
      ++cnt;
    }
  }
  for (int i = 1; i < cnt; ++i) ans = (ans << 1) % MOD;
  printf("%I64d", ans);
}
