#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100005];
int visited[100005];
int DFS(int index) {
  for (int i = 0; i < g[index].size(); i++) {
    pair<int, int> p = g[index][i];
    if (visited[p.first] == 0) {
      if (p.second)
        visited[p.first] = visited[index];
      else
        visited[p.first] = -1 * visited[index];
      if (!DFS(p.first)) return 0;
    } else {
      if (p.second == 0 && visited[p.first] == visited[index]) return 0;
      if (p.second == 1 && visited[p.first] != visited[index]) return 0;
    }
  }
  return 1;
}
int main() {
  int n, m, a, b, c, ans = 0;
  memset(visited, 0, sizeof visited);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      ans++;
      visited[i] = 1;
      if (!DFS(i)) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  int long long val = 1;
  for (int i = 0; i < ans - 1; i++) val = (val * 2) % 1000000007;
  cout << val << endl;
  return 0;
}
