#include <bits/stdc++.h>
using namespace std;
vector<int> graph[2][100009];
int vis[100009];
bool dfs(int i, bool love) {
  if (vis[i] != -1) return (love == vis[i]);
  vis[i] = love;
  for (int f = 0; f < graph[0][i].size(); f++)
    if (!dfs(graph[0][i][f], love)) return 0;
  for (int f = 0; f < graph[1][i].size(); f++)
    if (!dfs(graph[1][i][f], !love)) return 0;
  return 1;
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int f = 0; f < q; f++) {
    int a, b, c;
    cin >> a >> b >> c;
    graph[!c][a].push_back(b);
    graph[!c][b].push_back(a);
  }
  memset(vis, -1, sizeof vis);
  int ans = 0;
  for (int f = 1; f <= n; f++) {
    if (vis[f] == -1) {
      if (!dfs(f, 1)) {
        cout << 0 << endl;
        return 0;
      }
      ans++;
    }
  }
  long long ans1 = 1, mod = 1e9 + 7;
  for (int f = 1; f < ans; f++) ans1 = (ans1 * 2) % mod;
  cout << ans1 << endl;
}
