#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
const int P = 1000000007;
int H[N], to[N], type[N], nxt[N], e = 1;
void addedge(int t, int v, int s) {
  e++, to[e] = v, type[e] = s, nxt[e] = H[t], H[t] = e;
  e++, to[e] = t, type[e] = s, nxt[e] = H[v], H[v] = e;
}
int vis[N];
void dfs(int u, int t) {
  vis[u] = t;
  for (int i = H[u], v = to[i]; i; i = nxt[i], v = to[i]) {
    if (vis[v] == -1)
      dfs(v, t ^ 1 ^ type[i]);
    else if (vis[v] != (t ^ 1 ^ type[i])) {
      puts("0");
      exit(0);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int t, v, s;
    scanf("%d%d%d", &t, &v, &s);
    addedge(t, v, s);
  }
  int c = 0;
  for (int i = 1; i <= n; i++) vis[i] = -1;
  for (int i = 1; i <= n; i++)
    if (vis[i] == -1) c++, dfs(i, 0);
  int ans = 1;
  for (int i = 2; i <= c; i++) ans = ans * 2 % P;
  cout << ans << endl;
  return 0;
}
