#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > g;
vector<int> c;
bool isb;
void go(int i, int col) {
  c[i] = col;
  for (int j = 0; j < g[i].size(); j++) {
    if (!isb || (g[i][j].second == 0 && c[g[i][j].first] == col) ||
        (g[i][j].second == 1 && c[g[i][j].first] == !col)) {
      isb = 0;
      return;
    }
    if (c[g[i][j].first] == -1) go(g[i][j].first, g[i][j].second ? col : !col);
  }
}
long long mod = 1e9 + 7;
long long pw(long long b, long long p) {
  if (!p) return 1;
  if (p == 1) return b;
  if (p % 2) return (b * pw((b * b) % mod, (p - 1) / 2) % mod) % mod;
  return pw((b * b) % mod, p / 2) % mod;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    g.clear();
    g.resize(n);
    for (int i = 0; i < m; i++) {
      int x, y, c;
      scanf("%d %d %d", &x, &y, &c);
      ;
      g[x - 1].push_back({y - 1, c});
      g[y - 1].push_back({x - 1, c});
    }
    c.clear();
    c.resize(n, -1);
    isb = 1;
    int comp = 0;
    for (int i = 0; i < n; i++) {
      if (c[i] == -1) go(i, 1), comp++;
    }
    if (!isb)
      cout << 0 << endl;
    else
      cout << pw(2, comp - 1) << endl;
  }
  return 0;
}
