#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int maxn = 100000 + 123;
vector<pair<int, int>> G[maxn];
int father[maxn];
int n, m;
int getfather(int a) {
  return a == father[a] ? a : father[a] = getfather(father[a]);
}
int nv[maxn];
bool isvaild(int a, int v) {
  nv[a] = v;
  for (auto p : G[a]) {
    int tv(p.second ? v : -v);
    if (nv[p.first] == 0 && !isvaild(p.first, tv)) return false;
    if (nv[p.first] != 0 && nv[p.first] != tv) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  iota(father + 1, father + n + 1, 1);
  fill(nv + 1, nv + n + 1, 0);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    father[getfather(a)] = getfather(b);
  }
  for (int i = 1; i <= n; ++i)
    if (nv[i] == 0 && !isvaild(i, 1)) return puts("0"), 0;
  int cnt(0);
  for (int i = 1; i <= n; ++i)
    if (getfather(i) == i) cnt++;
  int res(1);
  for (int i = 1; i < cnt; ++i) res = (res * 2) % mod;
  printf("%d\n", res % mod);
  return 0;
}
