#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, NMAX = 100010;
int N, M;
int color[NMAX];
vector<int> Left[NMAX], Right[NMAX];
void DFS(int node, int current_color) {
  if (color[node] && color[node] != current_color) {
    cout << "0\n";
    exit(0);
  }
  if (color[node] == 0) {
    color[node] = current_color;
    for (int i = 0; i < int(Left[node].size()); ++i)
      DFS(Left[node][i], current_color);
    for (int i = 0; i < int(Right[node].size()); ++i)
      DFS(Right[node][i], 3 - current_color);
  }
}
int FastExponentiation(int exponent) {
  int base = 2, ret = 1;
  while (exponent) {
    if (exponent & 1) ret = (int64_t(ret) * base) % MOD;
    base = (int64_t(base) * base) % MOD;
    exponent >>= 1;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  int i, x, y, c;
  cin >> N >> M;
  while (M--) {
    cin >> x >> y >> c;
    c == 1 ? (Left[x].push_back(y), Left[y].push_back(x))
           : (Right[x].push_back(y), Right[y].push_back(x));
  }
  int components = 0;
  for (i = 1; i <= N; ++i) {
    if (color[i]) continue;
    DFS(i, 1);
    ++components;
  }
  cout << FastExponentiation(components - 1) << '\n';
  return 0;
}
