#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265359;
const double E = 2.718281828459;
vector<pair<int, int> > ad[100002];
int color[100002];
int padre[100002];
long long pot2[100002];
int Find(int u) {
  if (u == padre[u]) return u;
  return padre[u] = Find(padre[u]);
}
void UF(int a, int b) {
  int ap = Find(a);
  int bp = Find(b);
  if (ap == bp) return;
  padre[ap] = bp;
}
bool bicoloring(int u) {
  queue<int> q;
  bool isbipartite = 1;
  if (ad[u].empty()) return 1;
  q.push(u);
  color[u] = 1;
  int frente;
  while (isbipartite && !q.empty()) {
    frente = q.front();
    q.pop();
    for (int i = 0; i < ad[frente].size(); i++) {
      int v = ad[frente][i].first;
      int op = ad[frente][i].second;
      if (op == 1) {
        if (color[v] == 0) {
          color[v] = color[frente];
          q.push(v);
        } else if (color[v] == -color[frente]) {
          isbipartite = false;
          break;
        }
      } else {
        if (color[v] == 0) {
          color[v] = -color[frente];
          q.push(v);
        } else if (color[v] == color[frente]) {
          isbipartite = false;
          break;
        }
      }
    }
  }
  return isbipartite;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  pot2[0] = 1;
  for (int i = 1; i < 100002; i++) pot2[i] = (pot2[i - 1] * 2) % 1000000007;
  for (int i = 0; i < n; i++) padre[i] = i;
  int a, b, c;
  bool okbip = 1;
  int ap, bp;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    --a, --b;
    if (c == 0) {
      ad[a].push_back(make_pair((b), (-1)));
      ad[b].push_back(make_pair((a), (-1)));
    } else {
      ad[a].push_back(make_pair((b), (1)));
      ad[b].push_back(make_pair((a), (1)));
    }
    UF(a, b);
  }
  bool temp;
  for (int i = 0; i < n; i++) {
    if (color[i] == 0) {
      temp = bicoloring(i);
      okbip = okbip && temp;
      if (!okbip) break;
    }
  }
  int coloreado = 0, Ncomp = 0;
  for (int i = 0; i < n; i++) {
    if (color[Find(i)] != 0) coloreado++;
    if (padre[i] == i) Ncomp++;
  }
  int r = n - coloreado;
  Ncomp -= r;
  long long Fpart, ans;
  if (m == 0)
    Fpart = pot2[r - 1];
  else
    Fpart = pot2[r];
  if (!okbip)
    ans = 0;
  else if (m != 0)
    ans = (Fpart * pot2[Ncomp - 1]) % 1000000007;
  else
    ans = Fpart;
  printf("%I64d\n", ans);
  return 0;
}
