#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
using namespace std;
template <typename T>
void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}
template <typename T>
void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}
const long long N = 100 * 1000 + 228;
const long long MOD = 1e9 + 7;
vector<long long> g[N];
bool used[N];
long long color[N], C[N];
void dfs(long long v, long long cc) {
  C[v] = cc;
  used[v] = 1;
  for (long long to : g[v]) {
    if (!used[to]) dfs(to, cc);
  }
}
bool BIP = 1;
void DFS(long long v, long long col = 1) {
  color[v] = col;
  for (long long to : g[v]) {
    if (!color[to])
      DFS(to, 3 - col);
    else if (color[to] == color[v])
      BIP = 0;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > hate;
  for (long long i = 0; i < m; ++i) {
    long long u, v, c;
    cin >> u >> v >> c;
    if (c == 1)
      g[u].push_back(v), g[v].push_back(u);
    else
      hate.emplace_back(u, v);
  }
  long long comp = 0;
  for (long long i = 1; i <= n; ++i) {
    if (!used[i]) {
      dfs(i, ++comp);
    }
  }
  for (long long i = 1; i <= n; ++i) g[i].clear();
  for (auto p : hate) {
    long long a = C[p.first], b = C[p.second];
    if (a == b) {
      cout << 0 << '\n';
      return 0;
    }
    g[a].push_back(b), g[b].push_back(a);
  }
  memset(used, 0, sizeof used);
  long long CC = 0;
  n = comp;
  for (long long i = 1; i <= n; ++i) {
    if (!color[i]) {
      DFS(i);
      ++CC;
    }
  }
  if (!BIP) {
    cout << 0 << '\n';
    return 0;
  }
  long long res = 0;
  long long pw = 1;
  for (long long i = 0; i < CC - 1; ++i) pw = (pw * 2) % MOD;
  res += pw;
  cout << res << '\n';
  return 0;
}
