#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
int chk = 1, n, m, vis[mx], ans = 1, mod = 1e9 + 7;
vector<pair<int, int> > g[mx];
void dfs(int u, int par) {
  vis[u] = par;
  for (int i = int(0); i <= int(g[u].size() - 1); ++i) {
    int v = g[u][i].first, ed = g[u][i].second, npar = par;
    if (ed == 0) npar = 3 - par;
    if (vis[v] and vis[v] != npar)
      chk = 0;
    else if (!vis[v])
      dfs(v, npar);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = int(1); i <= int(m); ++i) {
    int u, v, c;
    scanf("%d %d %d", &u, &v, &c);
    g[u].push_back(make_pair(v, c));
    g[v].push_back(make_pair(u, c));
  }
  int cmp = 0;
  for (int i = int(1); i <= int(n); ++i) {
    if (!vis[i]) {
      dfs(i, 1);
      cmp++;
      if (cmp > 1) ans = (ans * 2) % mod;
    }
  }
  printf("%d\n", (chk) ? ans : 0);
  return 0;
}
