#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 200100;
int fa[maxn];
int find(int p) {
  if (p == fa[p])
    return p;
  else
    return fa[p] = find(fa[p]);
}
void bin(int a, int b) {
  int x1 = find(a);
  int x2 = find(b);
  if (x1 != x2) fa[x2] = x1;
}
long long powermi(long long n, long long p) {
  long long tt = n, k = 1;
  long long ans = 1;
  while (p) {
    if (p & 1) ans = ans * tt % mod;
    p /= 2;
    tt = tt * tt % mod;
  }
  return ans;
}
int main() {
  bool flag = false;
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++) fa[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (c == 1) {
      if (find(a) == find(b + n) || find(b) == find(a + n)) {
        flag = true;
        continue;
      }
      bin(a, b);
      bin(a + n, b + n);
    } else {
      if (find(a) == find(b) || find(a + n) == find(b + n)) {
        flag = true;
        continue;
      }
      bin(a, b + n);
      bin(a + n, b);
    }
  }
  long long ans = 1;
  if (flag) ans = 0;
  long long cnt = 0;
  for (int i = 1; i <= 2 * n; i++) {
    if (fa[i] == i) cnt++;
  }
  cnt /= 2;
  cnt--;
  ans = ans * powermi(2LL, cnt);
  cout << ans << endl;
  return 0;
}
