#include <bits/stdc++.h>
using namespace std;
vector<int> a[100001];
vector<int> b[100001];
int c[100001];
bool dfs(int x) {
  for (vector<int>::iterator it = a[x].begin(); it != a[x].end(); ++it) {
    int y = *it;
    if (c[y] == 0) {
      c[y] = c[x];
      if (!dfs(y)) return false;
    } else if (c[y] != c[x])
      return false;
  }
  for (vector<int>::iterator it = b[x].begin(); it != b[x].end(); ++it) {
    int y = *it;
    if (c[y] == 0) {
      c[y] = 3 - c[x];
      if (!dfs(y)) return false;
    } else if (c[y] == c[x])
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int x, y, boja;
    cin >> x >> y >> boja;
    --x;
    --y;
    if (boja == 1) {
      a[x].push_back(y);
      a[y].push_back(x);
    } else {
      b[x].push_back(y);
      b[y].push_back(x);
    }
  }
  int sol = 1;
  for (int i = 0; i < n; ++i)
    if (c[i] == 0) {
      c[i] = 1;
      if (dfs(i)) {
        if (i > 0) sol = sol * 2 % (1000 * 1000 * 1000 + 7);
      } else
        sol = 0;
    }
  cout << sol << endl;
  return 0;
}
