#include <bits/stdc++.h>
using namespace std;
int vis[1000005];
vector<int> vec[1000005];
vector<int> vec2[1000005];
vector<pair<int, int> > p;
int color[1000005];
int n, m, cmp;
int res, ans;
int a, b, c;
bool nosepuede = false;
void dfs(int u) {
  vis[u] = cmp;
  for (int i = 0; i < vec[u].size(); i++) {
    if (vis[vec[u][i]] == -1) dfs(vec[u][i]);
  }
}
void dfs2(int u, int c) {
  color[u] = c;
  for (int i = 0; i < vec2[u].size(); i++) {
    if (color[vec2[u][i]] == -1) {
      if (c == 1) dfs2(vec2[u][i], 0);
      if (c == 0) dfs2(vec2[u][i], 1);
    } else {
      if (color[vec2[u][i]] == c) nosepuede = true;
    }
  }
}
int main() {
  memset(vis, -1, sizeof vis);
  memset(color, -1, sizeof color);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    if (c == 1) {
      vec[a].push_back(b);
      vec[b].push_back(a);
    } else
      p.push_back(make_pair(a, b));
  }
  cmp = 1;
  for (int i = 0; i < n; i++) {
    if (vis[i] == -1) {
      dfs(i);
      cmp++;
    }
  }
  cmp--;
  ans = 1;
  for (int i = 0; i < p.size(); i++) {
    a = vis[p[i].first];
    b = vis[p[i].second];
    if (a == b) {
      cout << 0 << endl;
      return 0;
    }
    vec2[a].push_back(b);
    vec2[b].push_back(a);
  }
  for (int i = 1; i < cmp + 1; i++) {
    if (color[i] == -1) {
      dfs2(i, 0);
      ans++;
    }
  }
  ans--;
  if (nosepuede)
    cout << 0 << endl;
  else {
    res = 1;
    for (int i = 0; i < ans - 1; i++) res = (res * 2) % 1000000007;
    cout << res << endl;
  }
}
