#include <bits/stdc++.h>
using namespace std;
template <typename T, typename L>
bool smax(T &x, L y) {
  return x < y ? (x = y, true) : false;
}
template <typename T, typename L>
bool smin(T &x, L y) {
  return y < x ? (x = y, true) : false;
}
int pw(int, long long);
void dfs(int, vector<int> &);
void dfs(int, bool);
void dfs(int);
void bfs(int);
const int MAXN = 1e5 + 15;
bool mark[MAXN], flag = 1;
vector<int> adj[MAXN], g[MAXN], vec;
vector<pair<int, int>> edge;
vector<vector<int>> comp;
int num[MAXN], col[MAXN];
map<int, bool> rex[MAXN];
int n, m;
int ret, st = -1, en = -1;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cerr << "HELLO WORLD :)\n";
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    if (c) {
      adj[u].push_back(v);
      adj[v].push_back(u);
    } else
      edge.push_back({u, v});
  }
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      dfs(i, vec);
      comp.push_back(vec);
      vec.clear();
    }
  for (auto rel : edge) {
    int u = rel.first, v = rel.second;
    if (num[u] == num[v]) flag = 0;
  }
  for (auto rel : edge) {
    int u = num[rel.first], v = num[rel.second];
    if (!rex[u][v]) {
      g[u].push_back(v);
      g[v].push_back(u);
      st = u, en = v;
      rex[u][v] = rex[v][u] = 1;
    }
  }
  memset(mark, 0, sizeof mark);
  memset(col, -1, sizeof col);
  int cnt = 0;
  for (int i = 0; i < (int)comp.size(); i++)
    if (!mark[i]) {
      dfs(i, 1);
      cnt++;
    }
  if (!flag) return cout << "0\n", false;
  ret = pw(2, cnt - 1);
  return cout << ret << '\n', false;
}
int pw(int p, long long v) {
  int mem = 1;
  for (; v; v >>= 1, p = 1ll * p * p % 1000000007)
    if (v & 1) mem = 1ll * p * mem % 1000000007;
  return mem;
}
void dfs(int v, vector<int> &pre) {
  pre.push_back(v);
  mark[v] = true;
  num[v] = comp.size();
  for (auto u : adj[v])
    if (!mark[u]) dfs(u, pre);
}
void dfs(int v, bool c) {
  col[v] = c;
  mark[v] = 1;
  for (auto u : g[v]) {
    if (!mark[u]) dfs(u, c ^ 1);
    if (col[u] == col[v]) flag = 0;
  }
}
