#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int col[N], par[N], n, m;
bool odd;
vector<int> g[N];
int find(int x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
void merge(int x, int y) {
  if (find(x) != find(y)) par[find(x)] = find(y);
}
void dfs(int v, int c) {
  if (col[v] != -1) {
    if (col[v] != c) odd = 1;
    return;
  }
  col[v] = c;
  for (int i = 0; i < g[v].size(); i++) dfs(g[v][i], 1 - c);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    col[i] = -1;
  }
  vector<pair<int, int> > v;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    if (c)
      merge(a, b);
    else
      v.push_back(make_pair(a, b));
  }
  for (int i = 0; i < v.size(); i++)
    if (find(v[i].first) == find(v[i].second))
      odd = 1;
    else
      g[find(v[i].first)].push_back(find(v[i].second)),
          g[find(v[i].second)].push_back(find(v[i].first));
  int com = 0;
  for (int i = 0; i < n; i++)
    if (col[find(i)] == -1) dfs(find(i), 0), com++;
  if (odd) {
    cout << "0" << endl;
    return 0;
  }
  int res = 1ll;
  int MOD = 1e9 + 7;
  for (int i = 0; i < com - 1; i++) res = (res * 2ll) % MOD;
  cout << res << endl;
  return 0;
}
