#include <bits/stdc++.h>
using namespace std;
void bug() { cout << " " << endl; }
template <class T>
void bug(T a) {
  cout << a << "  ";
}
template <class T>
void bug(T* a, int n) {
  for (int i = 0; i < (n); i++) bug(a[i]);
}
int n, m, ans = 1;
vector<int> g[111111];
int vis[111111], col[111111];
void dfs(int u) {
  for (int i = 0; i < (g[u].size()); i++) {
    int v = g[u][i] / 2;
    int c = g[u][i] % 2;
    if (col[v] != -1 && col[v] != (c ^ col[u])) ans = 0;
    if (!vis[v]) {
      col[v] = c ^ col[u];
      vis[v] = 1;
      dfs(v);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < (m); i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c ^= 1;
    g[a].push_back(b * 2 + c);
    g[b].push_back(a * 2 + c);
  }
  memset(col, -1, sizeof(col));
  for (int i = 0; i < (n); i++)
    if (!vis[i]) {
      col[i] = 0;
      vis[i] = 1;
      dfs(i);
      if (i != 0) ans = ans * 2 % 1000000007;
    }
  cout << ans << endl;
  return 0;
}
