#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
int powMOD(int a, int b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    int t = powMOD(a, b / 2);
    return 1LL * t * t % MOD;
  } else {
    return 1LL * a * powMOD(a, b - 1) % MOD;
  }
}
struct DSU {
 private:
  vector<int> parent;

 public:
  DSU(int size) { parent.resize(size, -1); }
  int size_set(int vert) {
    vert = get_root(vert);
    return -parent[vert];
  }
  int get_root(int vert) {
    if (parent[vert] < 0) {
      return vert;
    }
    int root = get_root(parent[vert]);
    parent[vert] = root;
    return root;
  }
  bool unite(int v1, int v2) {
    v1 = get_root(v1);
    v2 = get_root(v2);
    if (v1 == v2) {
      return false;
    }
    assert(parent[v1] < 0 && parent[v2] < 0);
    if (parent[v1] < parent[v2]) {
      parent[v1] += parent[v2];
      parent[v2] = v1;
    } else {
      parent[v2] += parent[v1];
      parent[v1] = v2;
    }
    return true;
  }
};
bool dfs(int curr, const vector<vector<int>>& gr, vector<int>& colour,
         int c = 0) {
  colour[curr] = c;
  for (int next : gr[curr]) {
    if (colour[next] == -1) {
      if (!dfs(next, gr, colour, c ^ 1)) {
        return false;
      }
    } else if (colour[curr] == colour[next]) {
      return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  DSU dsu(n);
  int sets = n;
  vector<pair<int, int>> hate;
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    --u;
    --v;
    if (c == 1) {
      sets -= dsu.unite(u, v);
    } else {
      hate.push_back({u, v});
    }
  }
  vector<vector<int>> gr(n);
  for (const auto& edge : hate) {
    int uu = dsu.get_root(edge.first);
    int vv = dsu.get_root(edge.second);
    if (uu != vv) {
      gr[uu].push_back(vv);
      gr[vv].push_back(uu);
    } else {
      cout << "0\n";
      return 0;
    }
  }
  vector<int> colour(n, -1);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (colour[i] == -1 && dsu.get_root(i) == i) {
      ++cnt;
      if (!dfs(i, gr, colour)) {
        cout << "0\n";
        return 0;
      }
    }
  }
  cout << powMOD(2, cnt - 1) << "\n";
}
