#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a[100001];
vector<int> vis(100001, 0);
bool dfs(int node, int side) {
  vis[node] = side;
  for (__typeof((int)((a[node]).size())) i =
           (0) - ((0) > ((int)((a[node]).size())));
       i != ((int)((a[node]).size())) - ((0) > ((int)((a[node]).size())));
       i += 1 - 2 * ((0) > ((int)((a[node]).size())))) {
    int neigh = a[node][i].first;
    int edge = a[node][i].second;
    if (edge == 1) {
      if (vis[neigh] == 3 - side) return false;
      if (vis[neigh] == 0)
        if (!dfs(neigh, side)) return false;
    } else {
      if (vis[neigh] == side) return false;
      if (vis[neigh] == 0)
        if (!dfs(neigh, 3 - side)) return false;
    }
  }
  return true;
}
int main() {
  int n, k, c = 0;
  cin >> n >> k;
  while (k--) {
    int p, q, r;
    cin >> p >> q >> r;
    a[p].push_back(make_pair(q, r));
    a[q].push_back(make_pair(p, r));
  }
  for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
       i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) {
    if (vis[i] == 0) {
      if (!dfs(i, 1)) {
        cout << "0" << endl;
        return 0;
      }
      c++;
    }
  }
  c--;
  long long ans = 1;
  while (c--) {
    ans = (ans * 2) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}
