#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000 + 7;
const int N = 100007;
int n, m, comp, mark[N];
vector<pair<int, int> > G[N];
bool ok = true;
void dfs(int u, int col = 0) {
  mark[u] = col;
  for (auto p : G[u]) {
    int to = p.first;
    int t = p.second;
    if (mark[to] == -1)
      dfs(to, col ^ t);
    else {
      if (mark[to] != (col ^ t)) {
        ok = false;
        return;
      }
    }
  }
}
void solve() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int u, v, t;
    scanf("%d%d%d", &u, &v, &t);
    --u, --v;
    G[u].push_back(make_pair(v, 1 - t));
    G[v].push_back(make_pair(u, 1 - t));
  }
  memset(mark, -1, sizeof mark);
  for (int i = 0; i < (n); ++i) {
    if (mark[i] == -1) {
      ++comp;
      dfs(i);
    }
  }
  if (!ok)
    puts("0");
  else {
    int ans = 1;
    for (int i = 0; i < (comp - 1); ++i) {
      ans += ans;
      if (ans >= MOD) ans -= MOD;
    }
    printf("%d\n", ans);
  }
}
void testGen() {
  FILE *f = fopen("input.txt", "w");
  fclose(f);
}
int main() {
  cerr << fixed;
  cerr.precision(3);
  cout << fixed;
  cout.precision(9);
  solve();
  return 0;
}
