#include <bits/stdc++.h>
using namespace std;
template <class temp>
inline void Rd(temp &p) {
  p = 0;
  char c;
  while (c = getchar(), c < 48)
    ;
  do p = p * 10 + (c & 15);
  while (c = getchar(), c > 47);
}
int pow(int n, int m) {
  int ans = 1;
  while (m) {
    if (m & 1) ans = 1ll * ans * n % 1000000007;
    m >>= 1;
    n = 1ll * n * n % 1000000007;
  }
  return ans;
}
int head[100005], tot = 0;
struct eg {
  int v, col, nxt;
} E[100005 << 1];
void add_edge(int u, int v, int w) {
  E[++tot] = (eg){v, w, head[u]};
  head[u] = tot;
  E[++tot] = (eg){u, w, head[v]};
  head[v] = tot;
}
int col[100005];
bool dfs(int u) {
  for (int j = head[u]; j; j = E[j].nxt) {
    int v = E[j].v, c = E[j].col;
    if (!~col[v]) {
      col[v] = col[u] ^ c ^ 1;
      if (!dfs(v)) return false;
    } else {
      if ((col[u] ^ c ^ 1) != col[v]) return false;
    }
  }
  return true;
}
int main() {
  int n, m;
  Rd(n), Rd(m);
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    Rd(u), Rd(v), Rd(w);
    add_edge(u, v, w);
  }
  memset(col, -1, sizeof(col));
  int sz = 0;
  for (int i = 1; i <= n; i++)
    if (!~col[i]) {
      col[i] = 0;
      if (!dfs(i)) {
        puts("0");
        return 0;
      }
      ++sz;
    }
  printf("%d\n", pow(2, sz - 1));
}
