#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > graphe;
vector<bool> dejaVu;
vector<int> mod;
bool zero = false;
void detecte_bip(int id, int typ) {
  if (dejaVu[id]) {
    if (typ != mod[id]) zero = true;
    return;
  }
  dejaVu[id] = true;
  mod[id] = typ;
  for (int i = 0; i < graphe[id].size(); i++) {
    detecte_bip(graphe[id][i].first, (typ + graphe[id][i].second) % 2);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  graphe.resize(n);
  int t1, t2, type;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &t1, &t2, &type);
    t1--;
    t2--;
    graphe[t1].push_back(make_pair(t2, 1 - type));
    graphe[t2].push_back(make_pair(t1, 1 - type));
  }
  mod.resize(n);
  dejaVu.resize(n, false);
  int nbCompo = 0;
  for (int i = 0; i < n and not zero; i++) {
    if (not dejaVu[i]) {
      nbCompo++;
      detecte_bip(i, 0);
    }
  }
  if (zero) {
    printf("0\n");
    return 0;
  }
  int res = 1;
  for (int i = 0; i < nbCompo - 1; i++) res = (res * 2) % 1000000007;
  printf("%d\n", res);
  return 0;
}
