#include <bits/stdc++.h>
using namespace std;
int rnk[100001];
int parent[100001];
int size[100001];
int enemy[100001];
bool visited[100001];
void swap(int &a, int &b) {
  int temp = 0;
  temp = a;
  a = b;
  b = temp;
}
int find(int u) {
  if (parent[u] == u) return u;
  return parent[u] = find(parent[u]);
}
int mrg(int u, int v) {
  if (u == -1 || v == -1) return max(u, v);
  u = find(u);
  v = find(v);
  if (u == v) return u;
  if (rnk[u] > rnk[v]) swap(u, v);
  if (rnk[u] == rnk[v]) rnk[v]++;
  parent[u] = v;
  return v;
}
bool ack(int u, int v) {
  u = find(u);
  v = find(v);
  if (enemy[u] == v || enemy[v] == u) return false;
  int a = mrg(u, v);
  int b = mrg(enemy[u], enemy[v]);
  if (a == b) return false;
  enemy[a] = b;
  if (b != -1) enemy[b] = a;
  return true;
}
bool dis(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return false;
  int a = mrg(u, enemy[v]);
  int b = mrg(v, enemy[u]);
  if (a == b) return false;
  enemy[b] = a;
  enemy[a] = b;
  return true;
}
int main() {
  int n;
  int m;
  int a, b, k;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    rnk[i] = 1;
    enemy[i] = -1;
    visited[i] = false;
  }
  bool check = true;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &k);
    if (check) {
      if (k == 1) {
        if (!ack(a, b)) check = false;
      } else {
        if (!dis(a, b)) check = false;
      }
    }
  }
  int enemyCnt = 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    int p = find(i);
    if (!visited[p]) {
      if (enemy[p] == -1) {
        visited[p] = true;
        enemyCnt++;
      } else {
        cnt++;
        visited[p] = true;
        visited[find(enemy[p])] = true;
      }
    }
  }
  if (!check)
    printf("%d", 0);
  else {
    long long int num = 1;
    long long int num2 = 1;
    if (enemyCnt > 0) {
      cnt++;
    }
    while (cnt > 1) {
      num = num * 2;
      num = num % 1000000007;
      cnt--;
    }
    while (enemyCnt > 1) {
      num2 *= 2;
      num2 %= 1000000007;
      enemyCnt--;
    }
    printf("%lld", (num * num2) % 1000000007);
  }
  return 0;
}
