#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > graph[100001];
int visit[100001];
int flag = 0;
void dfs(int u, int c) {
  visit[u] = c;
  for (int i = 0; i < graph[u].size(); ++i) {
    int v = graph[u][i].first;
    int c = graph[u][i].second;
    if (visit[v] == -1)
      dfs(v, !(c ^ visit[u]));
    else if (visit[v] != !(c ^ visit[u]))
      flag = 1;
  }
}
long long f(long long exp) {
  long long base = 2;
  long long res = 1;
  while (exp) {
    if (exp & 1) res = res * base % 1000000007;
    exp = exp >> 1;
    base = base * base % 1000000007;
  }
  return res;
}
int main() {
  int n, m;
  int x, y, c;
  cin >> n >> m;
  memset(visit, -1, sizeof visit);
  for (int i = 0; i < m; ++i) {
    cin >> x >> y >> c;
    graph[x].push_back(make_pair(y, c));
    graph[y].push_back(make_pair(x, c));
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (visit[i] == -1) {
      ans++;
      dfs(i, 1);
    }
  }
  if (flag)
    cout << "0" << endl;
  else
    cout << f(ans - 1) << endl;
}
