#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int p[2][maxn];
vector<int> a[2][maxn];
void AddEdge(int u, int v, int w) {
  a[w][u].push_back(v);
  a[w][v].push_back(u);
}
int root(int *pa, int x) {
  int t = x;
  while (pa[t] > 0) {
    t = pa[t];
  }
  while (x != t) {
    int Q = pa[x];
    pa[x] = t;
    x = Q;
  }
  return t;
}
bool merge(int *pa, int x, int y) {
  x = root(pa, x), y = root(pa, y);
  if (x != y) {
    if (pa[x] < pa[y]) swap(x, y);
    pa[y] += pa[x];
    pa[x] = y;
  }
  return x != y;
}
int color[maxn];
bool check(int u) {
  for (__typeof(a[1][u].begin()) it = a[1][u].begin(); it != a[1][u].end();
       ++it) {
    int &v = *it;
    if (color[v] && color[v] != color[u]) return false;
    if (!color[v]) color[v] = color[u], check(v);
  }
  for (__typeof(a[0][u].begin()) it = a[0][u].begin(); it != a[0][u].end();
       ++it) {
    int &v = *it;
    if (color[v] && color[v] != 3 - color[u]) return false;
    if (!color[v]) color[v] = 3 - color[u], check(v);
  }
  return true;
}
int main(int argc, char const *argv[]) {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    memset(p, -1, sizeof p);
    for (int i = 1; i <= n; i++) a[0][i].clear();
    for (int i = 1; i <= n; i++) a[1][i].clear();
    int ans = 1;
    int lft = n;
    vector<pair<int, int> > t;
    while (m--) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      AddEdge(u, v, w);
      if (w == 1)
        lft -= merge(p[w], u, v);
      else {
        t.push_back(make_pair(u, v));
      }
    }
    int dis = 0;
    for (__typeof(t.begin()) it = t.begin(); it != t.end(); ++it) {
      int u = it->first, v = it->second;
      u = root(p[1], u);
      v = root(p[1], v);
      if (u == v) {
        ans = 0;
        break;
      }
      dis += merge(p[0], u, v);
    }
    lft -= dis;
    if (dis == n - 1) ans = 0;
    memset(color, 0, sizeof color);
    for (int i = 1; i <= n; i++) {
      if (!ans) break;
      if (!color[i]) {
        color[i] = 1;
        ans *= check(i);
      }
    }
    --lft;
    for (int i = 1; i <= lft; i++) {
      ans = (ans + ans) % mod;
    }
    cout << ans << endl;
  }
  return 0;
}
