#include <bits/stdc++.h>
using namespace std;
struct P {
  int u, w;
};
vector<P> G[100010];
int n, m;
int vis[100010];
bool dfs(int u, int fa) {
  for (int i = 0; i < G[u].size(); i++) {
    if (G[u][i].u == fa) continue;
    int v = G[u][i].u;
    int w = G[u][i].w;
    if (vis[v] == -1) {
      vis[v] = vis[u] ^ 1 ^ w;
      if (!dfs(v, u)) return false;
    } else if ((vis[v] == vis[u] && !w) || (vis[v] != vis[u] && w))
      return false;
  }
  return true;
}
int main() {
  scanf("%d %d", &n, &m);
  int x, y, w;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &x, &y, &w);
    G[x].push_back((P){y, w});
    G[y].push_back((P){x, w});
  }
  memset(vis, -1, sizeof(vis));
  long long ans = -1;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      vis[i] = 0;
      if (dfs(i, -1)) {
        if (ans == -1)
          ans = 1;
        else
          ans = ans * 2 % 1000000007;
      } else {
        ans = 0;
        break;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
