#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int M = 2;
struct Node {
  int parent, rela;
  void set(int p, int r) {
    parent = p;
    rela = r;
  }
} node[maxn];
int Find(int x) {
  int tmp_p;
  if (node[x].parent != x) {
    tmp_p = node[x].parent;
    node[x].parent = Find(node[x].parent);
    node[x].rela = (node[x].rela + node[tmp_p].rela) % M;
  }
  return node[x].parent;
}
void Merge(int a, int b, int p, int q, int d) {
  node[p].parent = q;
  node[p].rela = (node[b].rela - node[a].rela - d + M) % M;
}
int n, m;
const long long mod = 1e9 + 7;
void init() {
  for (int i = 0; i < maxn; ++i) {
    node[i].parent = i, node[i].rela = 0;
  }
}
long long powerMod(int x, int k) {
  long long res = 1;
  long long base = x;
  while (k) {
    if (k & 1) res = res * base % mod;
    base = base * base % mod;
    k >>= 1;
  }
  return res;
}
int main() {
  init();
  int num = 0;
  cin >> n >> m;
  int res = 1;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    int p = Find(a);
    int q = Find(b);
    if (p != q)
      Merge(a, b, p, q, c ^ 1);
    else {
      if ((node[a].rela - node[b].rela + 2) % 2 != 1 - c) res = 0;
    }
  }
  if (res == 0)
    cout << res << endl;
  else {
    for (int i = 1; i <= n; ++i)
      if (Find(i) == i) num++;
    cout << powerMod(2, num - 1) << endl;
  }
  return 0;
}
