#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int n, m, vis[100005] = {0}, f = 0;
vector<pair<int, int> > v[100005];
void dfs(int at) {
  if (vis[at] == 0) vis[at] = 1;
  for (__typeof(v[at].begin()) it = v[at].begin(); it != v[at].end(); it++) {
    int c = (*it).second, x = (*it).first;
    if (vis[x] == 0) {
      if (c == 1) {
        vis[x] = vis[at];
        dfs(x);
      } else {
        if (vis[at] == 1) vis[x] = 2;
        if (vis[at] == 2) vis[x] = 1;
        dfs(x);
      }
    }
    if (vis[x] != vis[at] && c == 1) f = 1;
    if (vis[x] == vis[at] && c == 0) f = 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  int cmp = 0;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      cmp++;
    }
  }
  if (f) {
    cout << "0";
    return 0;
  }
  long long int ans = 1;
  for (int i = 1; i <= cmp - 1; i++) ans = (ans * 2) % mod;
  cout << ans;
  return 0;
}
