#include <bits/stdc++.h>
using namespace std;
int n, m;
const int MAXN = 200001;
vector<int> hate[MAXN];
vector<int> love[MAXN];
int vis[MAXN];
int color[MAXN];
const int mod = 1e9 + 7;
void dfs(int here, int c) {
  color[here] = c;
  for (int i = 0; i < love[here].size(); i++) {
    int there = love[here][i];
    if (color[there] != -1 && color[there] != color[here]) {
      cout << 0;
      exit(0);
    } else if (color[there] == -1)
      dfs(there, c);
  }
  for (int i = 0; i < hate[here].size(); i++) {
    int there = hate[here][i];
    if (color[there] != -1 && color[there] == color[here]) {
      cout << 0;
      exit(0);
    } else if (color[there] == -1)
      dfs(there, c ^ 1);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  memset(color, -1, sizeof(color));
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 1) {
      love[a].push_back(b);
      love[b].push_back(a);
    } else {
      hate[a].push_back(b);
      hate[b].push_back(a);
    }
  }
  int cmp = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == -1) {
      dfs(i, 0);
      cmp++;
    }
  }
  long long ret = 1;
  for (int i = 1; i <= cmp - 1; i++) {
    ret *= 2;
    ret %= mod;
  }
  cout << ret;
  return 0;
}
