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