#include <bits/stdc++.h>
using namespace std;
bool ok = true;
int d[100005], N, M;
vector<vector<pair<int, bool>>> gr;
void dfs(int node, int val) {
  int newVal;
  d[node] = val;
  for (pair<int, bool> i : gr[node]) {
    newVal = val;
    if (val == 1 && i.second == false) {
      newVal = 2;
    } else if (val == 2 && i.second == false) {
      newVal = 1;
    }
    if (d[i.first] == 0) {
      dfs(i.first, newVal);
    } else if (d[i.first] != newVal) {
      ok = false;
    }
  }
}
void read() {
  cin >> N >> M;
  int x, y;
  bool l;
  gr.resize(N + 1);
  for (int i = 0; i < M; ++i) {
    cin >> x >> y >> l;
    gr[x].emplace_back(y, l);
    gr[y].emplace_back(x, l);
  }
}
int power(int exp) {
  int pw = 1;
  for (int i = 0; i < exp; ++i) {
    pw = pw * 2;
    if (pw >= 1000000007) {
      pw = pw % 1000000007;
    }
  }
  return pw;
}
int main() {
  int con = 0;
  read();
  for (int i = 1; i <= N; ++i) {
    if (d[i] == 0) {
      dfs(i, 1);
      ++con;
    }
  }
  if (ok == false) {
    cout << 0 << "\n";
  } else {
    cout << power(con - 1) << "\n";
  }
  return 0;
}
