#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void rd(Tp &x) {
  x = 0;
  int fh = 1;
  char ch = 1;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') fh = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename Tp>
inline void bird(Tp &x) {
  x = 0;
  int fh = 1;
  char ch = 1;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') fh = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 2 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename Tp>
inline void smax(Tp &x, Tp y) {
  if (x < y) x = y;
}
const int maxn = 100000 + 7;
const int maxm = 200000 + 7;
const int mod = 1000000007;
int n, m;
int Head[maxn], to[maxm], Next[maxm], w[maxm], tot;
void add(int x, int y, int z) {
  to[++tot] = y, Next[tot] = Head[x], Head[x] = tot, w[tot] = z;
}
inline void Init(void) {
  rd(n);
  rd(m);
  for (int i = 1, x, y, z; i <= m; i++) {
    rd(x);
    rd(y);
    rd(z);
    add(x, y, z);
    add(y, x, z);
  }
}
bool vis[maxn];
int c[maxn];
void dfs(int x, int f, int col) {
  if (vis[x]) {
    if (c[x] != col) {
      puts("0");
      exit(0);
    }
    return;
  }
  vis[x] = true;
  c[x] = col;
  for (int i = Head[x]; i; i = Next[i]) {
    int y = to[i];
    if (y == f) continue;
    if (w[i])
      dfs(y, x, col);
    else
      dfs(y, x, 3 - col);
  }
}
int fpow(long long x, int p) {
  long long res = 1ll;
  while (p) {
    if (p & 1) res = res * x % mod;
    p >>= 1;
    x = x * x % mod;
  }
  return res;
}
inline void Work(void) {
  long long ans = 1ll;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      dfs(i, 0, 1);
      ans = ans * 2 % mod;
    }
  }
  ans = ans * fpow(2, mod - 2) % mod;
  printf("%lld\n", ans);
}
signed main(void) {
  Init();
  Work();
  return 0;
}
