#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int m, n, cnt, fa[maxn], pos[maxn], a[maxn], b[maxn], ptr, color[maxn];
bool vis[maxn];
vector<int> g[maxn];
int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x])); }
void join(int x, int y) {
  int px = find(x), py = find(y);
  if (px == py) return;
  fa[px] = py;
}
bool dfs(int x, int now) {
  color[x] = now;
  for (int i = 0; i < g[x].size(); i++) {
    int& y = g[x][i];
    if (color[y]) {
      if (color[y] == now) return false;
    } else if (!dfs(y, 3 - now))
      return false;
  }
  return true;
}
void dfs2(int x) {
  vis[x] = 1;
  for (int i = 0; i < g[x].size(); i++) {
    int& y = g[x][i];
    if (!vis[y]) dfs2(y);
  }
}
long long pow(int k) {
  if (k == 0) return 1;
  if (k == 1) return 2;
  long long ans = pow(k >> 1);
  ans = ans * ans % 1000000007;
  if (k % 2) ans = (ans << 1) % 1000000007;
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    if (c == 0) {
      if (find(x) == find(y)) {
        cout << 0 << endl;
        return 0;
      }
      a[++ptr] = x;
      b[ptr] = y;
    } else
      join(x, y);
  }
  for (int i = 1; i <= ptr; i++) {
    int &x = a[i], &y = b[i];
    g[find(x)].push_back(find(y));
    g[find(y)].push_back(find(x));
  }
  for (int i = 1; i <= n; i++)
    if (!color[find(i)]) {
      if (!dfs(find(i), 1)) {
        cout << "0" << endl;
        return 0;
      }
    }
  for (int i = 1; i <= n; i++)
    if (!vis[find(i)]) {
      cnt++;
      dfs2(find(i));
    }
  cout << pow(cnt - 1) << endl;
  return 0;
}
