#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e5 + 5;
const long long int mod = 1e9 + 7;
long long int parent[maxn];
long long int sz[maxn];
void make_set(long long int v) {
  sz[v] = 1;
  parent[v] = v;
}
long long int find_set(long long int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_set(long long int a, long long int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b) return;
  if (sz[a] < sz[b]) swap(a, b);
  parent[b] = a;
  sz[a] += sz[b];
}
vector<pair<long long int, long long int>> hates;
set<long long int> g[maxn];
set<long long int> reps;
long long int color[maxn];
bool dfs(long long int node, long long int toColor) {
  if (color[node]) return (color[node] == toColor);
  color[node] = toColor;
  toColor = (toColor == 1 ? 2 : 1);
  for (auto& itr : g[node])
    if (!dfs(itr, toColor)) return false;
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) make_set(i);
  for (long long int i = 0; i < m; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    if (c == 1)
      union_set(a, b);
    else
      hates.push_back({a, b});
  }
  for (auto& itr : hates) {
    long long int u = find_set(itr.first), v = find_set(itr.second);
    if (u == v) {
      cout << "0";
      return 0;
    }
    g[u].insert(v);
    g[v].insert(u);
  }
  for (long long int i = 1; i <= n; i++) reps.insert(find_set(i));
  long long int component = 0;
  for (auto& itr : reps)
    if (color[itr] == 0) {
      if (!dfs(itr, 1)) {
        cout << "0\n";
        return 0;
      } else
        component++;
    }
  component--;
  long long int ans = 1;
  while (component--) ans = (ans * 2) % mod;
  cout << ans;
  return 0;
}
