#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const double EXP = 2.7182818284590452;
const double Pi = 3.14159265;
const double EPS = 1e-13;
const int INF = 1000 * 1000 * 1000;
const long long INFL = (long long)INF * (long long)INF;
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
long long poww(long long v, long long p) {
  if (p == 0) return 1;
  if (p & 1) {
    return (poww(v, p - 1) * v) % MOD;
  } else {
    long long t = poww(v, p / 2);
    return (t * t) % MOD;
  }
}
vector<int> G[100005];
int parent[100005];
vector<pair<int, int> > hates;
void Create(int i) { parent[i] = i; }
int GetParent(int v) {
  if (parent[v] == v) return v;
  return (parent[v] = GetParent(parent[v]));
}
bool IsOneSet(int v1, int v2) { return (GetParent(v1) == GetParent(v2)); }
void Union(int v1, int v2) {
  if (IsOneSet(v1, v2)) return;
  parent[GetParent(v1)] = GetParent(v2);
}
int n, m;
int t, a, b;
bool was[100005];
int color[100005];
long long number = 0;
bool colored(int v, int c) {
  if (color[v] == c) return true;
  if (color[v] != -1) return false;
  color[v] = c;
  bool is = true;
  for (int i = (0); i < (G[v].size()); ++i)
    is = is && colored(G[v][i], (c + 1) % 2);
  return is;
}
void accept() {
  memset(color, -1, sizeof(color));
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) Create(i);
  for (int i = (0); i < (m); ++i) {
    scanf("%d%d%d", &a, &b, &t);
    --a;
    --b;
    if (t == 1)
      Union(a, b);
    else
      hates.push_back(std::make_pair(a, b));
  }
  bool is = true;
  for (int i = (0); i < (hates.size()); ++i) {
    a = GetParent(hates[i].first);
    b = GetParent(hates[i].second);
    if (a == b) {
      is = false;
      break;
    }
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = (0); i < (n); ++i) {
    if (color[i] == -1 && GetParent(i) == i) {
      ++number;
      if (!colored(i, 1)) {
        is = false;
        break;
      }
    }
  }
  if (is) {
    cout << poww(2, number - 1);
  } else {
    cout << 0;
  }
}
int main(void) {
  accept();
  return 0;
}
