#include <bits/stdc++.h>
using namespace std;
struct DSU {
  int fa[102400];
  void init() {
    for (int i = 0; i < 102400; i++) fa[i] = i;
  }
  int gt(int x) {
    if (fa[x] == x) return x;
    return fa[x] = gt(fa[x]);
  }
  void unite(int x, int y) {
    x = gt(x);
    y = gt(y);
    if (x != y) fa[x] = y;
  }
} dsu;
vector<pair<int, int> > edge;
vector<int> adj[102400];
int c[102400];
void dfs(int p, int co) {
  c[p] = co;
  for (int i = 0; i < adj[p].size(); i++) {
    int x = adj[p][i];
    if (!c[x]) dfs(x, 3 - co);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  dsu.init();
  for (int i = 0; i < m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    if (w)
      dsu.unite(a, b);
    else
      edge.push_back(make_pair(a, b));
  }
  for (int i = 0; i < n; i++) c[i] = 100000000;
  for (int i = 0; i < n; i++) {
    c[dsu.gt(i)] = 0;
  }
  for (int i = 0; i < edge.size(); i++) {
    adj[dsu.gt(edge[i].first)].push_back(dsu.gt(edge[i].second));
    adj[dsu.gt(edge[i].second)].push_back(dsu.gt(edge[i].first));
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!c[i]) {
      dfs(i, 1);
      if (ans)
        ans = ans * 2 % 1000000007;
      else
        ans = 1;
    }
  }
  for (int i = 0; i < edge.size(); i++) {
    if (c[dsu.gt(edge[i].first)] == c[dsu.gt(edge[i].second)]) ans = 0;
  }
  cout << ans << endl;
  return 0;
}
