#include <bits/stdc++.h>
const int MOD = (int)1e9 + 7;
const int N = 100000 + 5;
int n, m;
std::vector<std::pair<int, int>> edges[N];
int dp[N][2];
bool vis[N];
bool solve(int source) {
  dp[source][0] = 0;
  std::queue<int> que;
  que.push(source);
  que.push(0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    int c = que.front();
    que.pop();
    vis[u] = true;
    for (auto t : edges[u]) {
      int v = t.first;
      int w = t.second;
      if (dp[v][c ^ 1] == -1) {
        dp[v][c ^ 1] = dp[u][c] ^ w;
        que.push(v);
        que.push(c ^ 1);
      } else {
        if (dp[u][c] ^ dp[v][c ^ 1] ^ w) {
          return false;
        }
      }
      if (dp[v][c] != -1 && (dp[v][c] ^ dp[u][c] ^ w) == 0) {
        return false;
      }
    }
  }
  return true;
}
int work() {
  memset(dp, -1, sizeof(dp));
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    if (vis[i]) continue;
    if (!solve(i)) {
      return 0;
    }
    tot++;
  }
  int ret = 1;
  for (int i = 0; i < tot - 1; ++i) {
    ret = ret * 1ll * 2 % MOD;
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    edges[a].emplace_back(b, c);
    edges[b].emplace_back(a, c);
  }
  printf("%d\n", work());
}
