#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > e[100005];
int vis[100005];
long long ans;
void dfs(int u) {
  int i, v, c;
  for (i = 0; i < e[u].size(); i++) {
    v = e[u][i].first;
    c = e[u][i].second;
    if (vis[v] == -1) {
      if (c)
        vis[v] = vis[u];
      else
        vis[v] = !vis[u];
      dfs(v);
    } else {
      if (c && vis[v] != vis[u])
        ans = 0;
      else if (!c && vis[v] == vis[u])
        ans = 0;
    }
  }
}
int main() {
  int i, j, n, m, a, b, c;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &c);
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  ans = (1000000007 + 1) / 2;
  memset(vis, -1, sizeof(vis));
  for (i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      ans = ans * 2 % 1000000007;
      vis[i] = 0;
      dfs(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
