#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T t) {
  cout << t << endl;
}
template <typename T, typename... Args>
void out(T t, Args... args) {
  cout << t << " ";
  out(args...);
}
const int N = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
int pre[N];
int find(int x) {
  if (x == pre[x]) return x;
  return pre[x] = find(pre[x]);
}
struct Edge {
  int a, b, c;
} e[N];
long long qpow(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ret;
}
vector<int> g[N];
int col[N];
bool two(int u, int c) {
  col[u] = c;
  for (auto v : g[u]) {
    if (col[u] == col[v] || (!col[v] && !two(v, -c))) return false;
  }
  return true;
}
void work() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (n); i++) pre[i] = i;
  for (int i = 0; i < (m); i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    e[i] = {a, b, c};
    if (c) {
      pre[find(a)] = find(b);
    }
  }
  bool imp = false;
  for (int i = 0; i < (m); i++) {
    if (e[i].c) continue;
    e[i].a = find(e[i].a);
    e[i].b = find(e[i].b);
    int a = e[i].a, b = e[i].b;
    if (a == b) {
      imp = true;
      break;
    }
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  int cnt = 0;
  for (int i = 1; i <= (n); i++) {
    int fi = find(i);
    if (!col[fi]) {
      ++cnt;
      if (!two(fi, 1)) imp = true;
    }
  }
  if (imp) {
    puts("0");
    return;
  }
  printf("%lld\n", qpow(2, cnt - 1));
}
int main() { work(); }
