#include <bits/stdc++.h>
using namespace std;
long long n, m;
const long long MOD = 1e9 + 7;
vector<vector<pair<int, int>>> g;
vector<bool> used;
vector<int> check;
int cnt;
void bfs(int s) {
  if (!g[s].size()) return;
  queue<int> q;
  cnt++;
  q.push(s);
  used[s] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i].first;
      int info = g[v][i].second;
      if (used[to]) {
        if (info == (check[v] ^ check[to])) {
          cout << 0;
          exit(0);
        }
      } else {
        used[to] = true;
        if (v == s)
          check[to] = info;
        else
          check[to] = (!(check[v] ^ info));
        q.push(to);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[--a].push_back(pair<int, int>(--b, c));
    g[b].push_back(pair<int, int>(a, c));
  }
  used.resize(n, false);
  check.resize(n, -1);
  cnt = -1;
  for (int i = 0; i < n; i++) {
    if (!used[i]) bfs(i);
  }
  for (int i = 0; i < n; i++)
    if (!used[i]) cnt++;
  long long ans = 1;
  for (int i = 0; i < cnt; i++) ans = (ans * 2) % MOD;
  cout << ans;
  cin >> n;
  return 0;
}
