#include <bits/stdc++.h>
using namespace std;
int par[100010], vis[100010], col[100010];
vector<int> gold[100010], g[100010];
void init(int n) {
  for (int i = 1; i <= n; ++i) {
    par[i] = i;
  }
}
int find(int u) { return par[u] = ((par[u] == u) ? (u) : find(par[u])); }
void join(int a, int b) {
  a = find(a);
  b = find(b);
  par[a] = b;
}
int isBipartite(int u, int color) {
  int y = 1;
  vis[u] = 1;
  col[u] = color;
  for (auto& v : g[u]) {
    if (vis[v]) {
      if (color == col[v]) y = 0;
    } else {
      if (!isBipartite(v, -color)) y = 0;
    }
  }
  return y;
}
signed main() {
  int n, m, ans = 1;
  scanf("%d", &n);
  scanf("%d", &m);
  init(n);
  while (m--) {
    int a, b, c;
    scanf("%d", &a);
    scanf("%d", &b);
    scanf("%d", &c);
    if (c) {
      join(a, b);
    } else {
      gold[a].push_back(b);
      swap(a, b);
      gold[a].push_back(b);
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (auto& v : gold[i]) {
      g[find(i)].push_back(find(v));
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[find(i)]) {
      ans *= isBipartite(find(i), 1) * min(i, 2);
      if (ans >= 1000000007) ans -= 1000000007;
    }
  }
  printf("%d\n", ans);
  return 0;
}
