#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, a, b, c, p[100010], cnt;
bool vis[100010], ed;
vector<pair<int, int> > V[100010];
int powmod(int x, int y) {
  int res = 1, cro = x;
  while (y) {
    if (y % 2) res = 1ll * res * cro % mod;
    cro = 1ll * cro * cro % mod;
    y /= 2;
  }
  return res;
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < V[x].size(); i++) {
    int y = V[x][i].first;
    int z = V[x][i].second;
    if (vis[y]) {
      if (p[y] != p[x] ^ z) ed = 1;
      continue;
    } else {
      p[y] = p[x] ^ z;
      vis[y] = 1;
      dfs(y);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &c);
    V[a].push_back(make_pair(b, c ^ 1));
    V[b].push_back(make_pair(a, c ^ 1));
  }
  ed = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      cnt++;
    }
  }
  if (ed)
    puts("0");
  else
    printf("%d", powmod(2, cnt - 1));
}
