#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 20);
vector<pair<int, int> > v[MX];
int state[MX];
int n, m;
void dfs(int x, int flag) {
  if (state[x]) {
    if (state[x] != flag) {
      puts("0");
      exit(0);
    }
    return;
  }
  state[x] = flag;
  for (auto pp : v[x]) dfs(pp.first, pp.second ? 3 - flag : flag);
}
int main() {
  scanf("%d %d", &n, &m);
  while (m--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    v[a].push_back({b, 1 - c});
    v[b].push_back({a, 1 - c});
  }
  int ans = 0;
  for (int j = 1; j <= n; j++)
    if (!state[j]) ++ans, dfs(j, 1);
  int p = 1;
  while (--ans) {
    p *= 2;
    p %= 1000000007;
  }
  cout << p << endl;
}
