#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, mod = 1e9 + 7;
int visF[N], col[N];
vector<int> v[2][N], cnt[N], a[N];
void dfsF(int x, int cn) {
  cnt[cn].push_back(x);
  visF[x] = cn;
  for (auto i : v[1][x])
    if (!visF[i]) dfsF(i, cn);
}
void dfsE(int x, int c) {
  col[x] = c;
  c *= -1;
  for (auto i : a[x]) {
    if (!col[i])
      dfsE(i, c);
    else if (col[i] == col[x]) {
      cout << 0;
      exit(0);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, type;
    scanf("%d%d%d", &x, &y, &type);
    v[type][x].push_back(y);
    v[type][y].push_back(x);
  }
  int cn = 0;
  for (int i = 1; i <= n; i++)
    if (!visF[i]) dfsF(i, ++cn);
  for (int cnn = 1; cnn <= cn; cnn++)
    for (auto j : cnt[cnn])
      for (auto k : v[0][j]) a[cnn].push_back(visF[k]);
  long long ans = 0;
  for (int i = 1; i <= cn; i++)
    if (!col[i]) {
      dfsE(i, 1);
      if (!ans)
        ans++;
      else
        ans = (ans * 2ll) % mod;
    }
  cout << ans;
}
