#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int n, m, prt[N];
vector<vector<int> > v;
vector<pair<int, int> > hate;
bool vis[N], col[N];
map<pair<int, int>, int> mpp;
int get(int u) {
  if (prt[u] == u) return u;
  return prt[u] = get(prt[u]);
}
void dfs(int u, int c) {
  col[u] = c ^ 1;
  vis[u] = 1;
  for (int i = 0; i < v[u].size(); ++i) {
    int x = v[u][i];
    if (!vis[x])
      dfs(x, col[u]);
    else if (col[x] == col[u]) {
      puts("0");
      exit(0);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) prt[i] = i;
  v.resize(n);
  for (int i = 0; i < m; ++i) {
    int x, y, l;
    scanf("%d %d %d", &x, &y, &l);
    x--, y--;
    if (l)
      prt[get(x)] = get(y);
    else
      hate.push_back(make_pair(x, y));
  }
  for (int i = 0; i < hate.size(); ++i) {
    int x = get(hate[i].first);
    int y = get(hate[i].second);
    if (mpp[{x, y}] == 0 && mpp[{y, x}] == 0) {
      v[x].push_back(y);
      v[y].push_back(x);
      mpp[{x, y}] = 1;
    }
  }
  int pw = 0;
  for (int i = 0; i < n; ++i) {
    if (!vis[i] && get(prt[i]) == i) {
      dfs(i, 0);
      pw++;
    }
  }
  long long ans = 1;
  for (int i = 1; i < pw; ++i) ans *= 2 * 1ll, ans %= mod;
  printf("%lld\n", ans);
  return 0;
}
