#include <bits/stdc++.h>
using namespace std;
int N, M;
int A[100009];
int B[100009];
int C[100009];
vector<int> G[100009];
int color[100009];
bool col(int s, int c) {
  color[s] = c;
  for (int i = 0; i < (int)G[s].size(); i++) {
    if (color[G[s][i]] == 0) {
      if (!col(G[s][i], -c)) return false;
    } else if (color[s] == color[G[s][i]]) {
      return false;
    }
  }
  return true;
}
int rot[100009];
int dep[100009];
int root(int i) {
  if (i == rot[i]) return i;
  return rot[i] = root(rot[i]);
}
void unite(int x, int y) {
  int X = root(x);
  int Y = root(y);
  if (X == Y) return;
  if (dep[X] > dep[Y]) {
    dep[X] += dep[Y];
    dep[Y] = 0;
    rot[X] = Y;
  } else {
    dep[Y] += dep[X];
    dep[X] = 0;
    rot[Y] = X;
  }
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &A[i], &B[i], &C[i]);
    A[i]--;
    B[i]--;
  }
  for (int i = 0; i < N; i++) {
    rot[i] = i;
    dep[i] = 1;
  }
  for (int i = 0; i < M; i++) {
    if (C[i] == 1) {
      unite(A[i], B[i]);
    }
  }
  for (int i = 0; i < M; i++) {
    if (C[i] == 0) {
      if (root(A[i]) == root(B[i])) {
        printf("0\n");
        return 0;
      }
    }
  }
  for (int i = 0; i < M; i++) {
    if (C[i] == 0) {
      G[root(A[i])].push_back(root(B[i]));
      G[root(B[i])].push_back(root(A[i]));
    }
  }
  for (int i = 0; i < N; i++) {
    sort(G[i].begin(), G[i].end());
    G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
  }
  for (int i = 0; i < N; i++) {
    if ((int)G[i].size() == 0) continue;
    if (color[i] != 0) continue;
    if (!col(i, 1)) {
      printf("0\n");
      return 0;
    }
  }
  for (int i = 0; i < M; i++) {
    if (C[i] == 0) {
      unite(A[i], B[i]);
    }
  }
  int ans = 500000004;
  for (int i = 0; i < N; i++) {
    if (rot[i] == i) {
      ans *= 2;
      ans %= 1000000007;
    }
  }
  printf("%d\n", ans);
  return 0;
}
