#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
const int MOD = 1E9 + 7;
vector<int> adj[MAXN];
int N, M;
vector<pair<int, int> > edges;
int par[MAXN];
int col[MAXN];
bool vis[MAXN];
void init() {
  for (int i = 0; i < N; ++i) {
    par[i] = i;
  }
}
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
bool join(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  par[a] = b;
  return true;
}
bool dfs(int cur, int ccol) {
  if (vis[cur]) {
    return col[cur] == ccol;
  }
  vis[cur] = true;
  col[cur] = ccol;
  bool ret = true;
  for (int nxt : adj[cur]) {
    ret &= dfs(nxt, ccol ^ 1);
  }
  return ret;
}
int main() {
  scanf("%d %d", &N, &M);
  init();
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --a, --b;
    if (c == 0) {
      edges.push_back(pair<int, int>(a, b));
    } else {
      join(a, b);
    }
  }
  for (pair<int, int> e : edges) {
    if (find(e.first) == find(e.second)) {
      printf("0\n");
      return 0;
    }
  }
  for (pair<int, int> e : edges) {
    int a = find(e.first);
    int b = find(e.second);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int cnt = 0;
  memset(col, -1, sizeof(col));
  for (int i = 0; i < N; ++i) {
    int cur = find(i);
    if (!vis[cur]) {
      ++cnt;
      if (!dfs(cur, 0)) {
        printf("0\n");
        return 0;
      }
    }
  }
  int res = 1;
  for (int i = 0; i < cnt - 1; ++i) {
    res = (res * 2) % MOD;
  }
  printf("%d\n", res);
  return 0;
}
