#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long n, m, cc;
long long p[100005];
vector<pair<long long, long long> > g[100005];
long long col[100005];
bool vis[100005];
void dfs(long long s, long long c) {
  if (vis[s]) return;
  vis[s] = true;
  col[s] = c;
  for (auto i : g[s]) {
    if (vis[i.first] == false) {
      if (i.second == 0) {
        dfs(i.first, 1 - c);
      } else {
        dfs(i.first, c);
      }
    } else {
      if (c == col[i.first] && !i.second) {
        cout << "0\n";
        exit(0);
      } else if (c != col[i.first] && i.second) {
        cout << "0\n";
        exit(0);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  for (long long i = 1; i <= n; i++) {
    if (vis[i] == false) {
      cc++;
      dfs(i, 0);
    }
  }
  long long ans = 1LL;
  for (long long i = 1; i < cc; i++) ans = (ans * 2) % 1000000007;
  cout << ans << endl;
  return 0;
}
