#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mo = 1e9 + 7;
int readint() {
  int x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    (s = getchar());
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    (s = getchar());
  }
  return x * f;
}
struct edge {
  int v, nxt;
  edge(){};
  edge(int V, int N) {
    v = V;
    nxt = N;
  }
} e[maxn << 1];
int head[maxn], edge_cnt = -1;
;
void addedge(int u, int v) {
  e[++edge_cnt] = edge(v, head[u]);
  head[u] = edge_cnt;
  e[++edge_cnt] = edge(u, head[v]);
  head[v] = edge_cnt;
}
int fa[maxn];
int vis[maxn];
vector<pair<int, int> > hate;
int findset(int x) {
  if (fa[x] != x) fa[x] = findset(fa[x]);
  return fa[x];
}
void dfs(int u, int val) {
  vis[u] = val;
  for (int i = head[u]; ~i; i = e[i].nxt) {
    int v = e[i].v;
    if (vis[v] == 1) {
      dfs(v, ~val);
    } else {
      if (~val != vis[v]) {
        printf("0");
        exit(0);
      }
    }
  }
}
int qkpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1ll * ans * a % mo;
    b >>= 1;
    a = 1ll * a * a % mo;
  }
  return ans;
}
int main() {
  memset(head, -1, sizeof(head));
  int n = readint(), m = readint();
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int u = readint(), v = readint(), w = readint();
    if (w == 1) {
      int fu = findset(u), fv = findset(v);
      fa[fu] = fv;
    } else {
      hate.push_back(make_pair(u, v));
      ;
    }
  }
  for (int i = 0; i < hate.size(); i++) {
    int u = hate[i].first, v = hate[i].second;
    int fu = findset(u), fv = findset(v);
    addedge(fu, fv);
  }
  for (int i = 1; i <= n; i++) {
    vis[i] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[findset(i)] == 1) {
      ans++;
      dfs(findset(i), 0);
    }
  }
  cout << qkpow(2, ans - 1);
  return 0;
}
