#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1677721600")
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const double pi = acos(-1.0);
const long long iinf = 0x3f3f3f3f3f3f3f3f;
int readT() {
  char c;
  int ret = 0, flg = 0;
  while (c = getchar(), (c < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    flg = 1;
  else
    ret = c ^ 48;
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
  return flg ? -ret : ret;
}
long long readTL() {
  char c;
  int flg = 0;
  long long ret = 0;
  while (c = getchar(), (c < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    flg = 1;
  else
    ret = c ^ 48;
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
  return flg ? -ret : ret;
}
int n, m;
int fa[maxn << 1];
void Init() {
  for (int i = 1; i <= n + n; i++) {
    fa[i] = i;
  }
}
int Find(int x) {
  if (fa[x] != x) fa[x] = Find(fa[x]);
  return fa[x];
}
void Union(int u, int v) {
  int fau = Find(u);
  int fav = Find(v);
  if (fau == fav) return;
  fa[fav] = fau;
}
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (b == 0) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= a / b * x;
  }
}
long long inv(long long a, long long n) {
  long long x, y, d;
  exgcd(a, n, d, x, y);
  return (x + n) % n;
}
int main() {
  n = readT();
  m = readT();
  Init();
  for (int i = (1); i <= (m); i++) {
    int u = readT();
    int v = readT();
    int x = readT();
    if (x) {
      if (Find(u + n) == Find(v)) {
        puts("0");
        return 0;
      }
      Union(u, v);
      Union(u + n, v + n);
    } else {
      if (Find(u) == Find(v)) {
        puts("0");
        return 0;
      }
      Union(u + n, v);
      Union(v + n, u);
    }
  }
  int cnt = 0;
  for (int i = (1); i <= (n + n); i++) {
    if (fa[i] == i) {
      cnt++;
    }
  }
  cnt /= 2;
  long long ans = 1;
  for (int i = (1); i <= (cnt); i++) {
    ans = (ans * 2) % mod;
  }
  ans = (ans * inv(2, mod)) % mod;
  cout << ans << endl;
  return 0;
}
