#include <bits/stdc++.h>
const int N = 100005;
const int MOD = 1000000007;
using namespace std;
int n, m;
vector<int> a[N], adj[N], sa[N];
vector<pair<int, int> > dis;
int lab[N], color[N];
bool was[N];
bool isBipartite;
void dfs(int u, int cc) {
  lab[u] = cc;
  for (int v : adj[u]) {
    if (!lab[v]) dfs(v, cc);
  }
}
void dfsBipartite(int u, int c) {
  color[u] = c;
  for (int v : sa[u]) {
    if (color[v] != -1) {
      isBipartite &= color[u] != color[v];
    } else {
      dfsBipartite(v, !c);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  int u, v, k;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v >> k;
    a[u].push_back(v);
    a[v].push_back(u);
    if (k == 1) {
      adj[u].push_back(v);
      adj[v].push_back(u);
    } else {
      dis.push_back({u, v});
    }
  }
  int cc = 0;
  for (int i = 1; i <= n; ++i) {
    if (!lab[i]) dfs(i, ++cc);
  }
  for (auto it : dis) {
    if (lab[it.first] == lab[it.second]) {
      cout << 0 << endl;
      return 0;
    }
    sa[lab[it.first]].push_back(lab[it.second]);
    sa[lab[it.second]].push_back(lab[it.first]);
  }
  for (int i = 1; i <= cc; ++i) color[i] = -1;
  isBipartite = 1;
  for (int i = 1; i <= cc; ++i) {
    if (color[i] == -1) {
      dfsBipartite(i, 0);
      if (!isBipartite) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  assert(isBipartite);
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    if (!was[i]) {
      if (i > 1) {
        ans += ans;
        if (ans >= MOD) ans -= MOD;
      }
      queue<int> Q;
      Q.push(i);
      was[i] = 1;
      while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for (int v : a[u])
          if (!was[v]) {
            was[v] = 1;
            Q.push(v);
          }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
