#include <bits/stdc++.h>
using namespace std;
long long N, M;
vector<vector<long long> > Graph;
bool Vis[1000005] = {0};
long long Val[1000005];
bool flag = 0;
long long Power(long long base, long long expo) {
  long long Ans = 1;
  while (expo > 0) {
    if (expo % 2 == 1) {
      Ans = (Ans * base) % 1000000007;
    }
    expo >>= 1;
    base = (base * base) % 1000000007;
  }
  return Ans % 1000000007;
}
void Bfs(long long n) {
  queue<long long> Q;
  Q.push(n);
  while (Q.size() > 0) {
    long long x = Q.front();
    Q.pop();
    for (long long m = 0; m < Graph[x].size(); ++m) {
      long long y;
      if (Graph[x][m] > 0) {
        y = Val[x];
      } else {
        y = !Val[x];
      }
      if (Vis[abs(Graph[x][m])] == 1) {
        if (y != Val[abs(Graph[x][m])]) {
          cout << 0 << endl;
          flag = 1;
          return;
        }
      } else {
        Val[abs(Graph[x][m])] = y;
        Q.push(abs(Graph[x][m]));
        Vis[abs(Graph[x][m])] = 1;
      }
    }
  }
  return;
}
int main() {
  scanf("%lld", &N);
  scanf("%lld", &M);
  Graph.resize(N + 5);
  long long x, y, z;
  for (long long m = 0; m < M; ++m) {
    scanf("%lld", &x);
    scanf("%lld", &y);
    scanf("%lld", &z);
    if (z == 0) {
      Graph[x].push_back(-y);
      Graph[y].push_back(-x);
    } else {
      Graph[x].push_back(y);
      Graph[y].push_back(x);
    }
  }
  long long Ans = 0;
  for (long long n = 1; n <= N; ++n) {
    if (Vis[n] == 0) {
      Ans++;
      Vis[n] = 1;
      Bfs(n);
      if (flag) {
        return 0;
      }
    }
  }
  Ans = Power(2, Ans - 1);
  cout << Ans << endl;
  return 0;
}
