#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct edge {
  int v, next, w;
} e[maxn << 1];
int head[maxn], cnt;
inline void add(int x, int y, int w) {
  e[++cnt].v = y;
  e[cnt].w = w;
  e[cnt].next = head[x];
  head[x] = cnt;
}
int n, m;
int ans;
int vis[maxn], col[maxn];
void dfs(int x, int fa, int w) {
  if (vis[x]) {
    if (col[x] != w) {
      cout << "0" << endl;
      exit(0);
    }
    return;
  }
  vis[x] = 1;
  col[x] = w;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].v;
    if (to == fa) continue;
    dfs(to, x, (e[i].w) ? w : -w);
  }
}
const int mod = 1e9 + 7;
long long qpow(long long a, int b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    add(x, y, z);
    add(y, x, z);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      ans++;
      dfs(i, 0, 1);
    }
  printf("%lld\n", qpow(2, ans - 1));
}
