#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
int n, m;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d%d", &n, &m);
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < (int)(m); i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    u--;
    v--;
    g[u].emplace_back(v, !w);
    g[v].emplace_back(u, !w);
  }
  vector<int> dist(n, -1);
  long long int ans = (MOD + 1) / 2;
  for (int i = 0; i < (int)(n); i++)
    if (dist[i] == -1) {
      queue<int> q;
      q.emplace(i);
      dist[i] = 0;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (pair<int, int> e : g[u]) {
          int v = e.first;
          int nc = (dist[u] + e.second) & 1;
          if (dist[v] == -1) {
            q.push(v);
            dist[v] = nc;
          } else if (dist[v] != nc) {
            puts("0");
            return 0;
          }
        }
      }
      ans = ans * 2 % MOD;
    }
  cout << ans << endl;
  return 0;
}
