#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5, MaxM = 2e5, Pt = 1e9 + 7;
int n, m, all;
int pre[MaxM + 5], last[MaxN + 5], other[MaxM + 5];
int father[MaxN + 5], col[MaxN + 5];
int a[MaxN + 5];
struct EDGE {
  int u, v, w;
} edge[MaxM + 5];
bool GG, vis[MaxN + 5];
map<pair<int, int>, bool> con;
int Find(int x) {
  if (father[x] == x) return x;
  return father[x] = Find(father[x]);
}
void Build(int x, int y) {
  pre[++all] = last[x];
  last[x] = all;
  other[all] = y;
}
void Init() {
  for (int i = 1; i <= n; i++) father[i] = i;
  GG = false;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
    if (edge[i].w == 1 && Find(edge[i].u) != Find(edge[i].v))
      father[Find(edge[i].u)] = Find(edge[i].v);
  }
  con.clear();
  all = -1;
  memset(last, -1, sizeof(last));
  for (int i = 1; i <= m; i++) {
    int u = edge[i].u, v = edge[i].v;
    if (edge[i].w == 0 && !con[make_pair(Find(u), Find(v))]) {
      if (Find(u) == Find(v)) {
        GG = true;
        break;
      }
      Build(Find(u), Find(v));
      Build(Find(v), Find(u));
      con[make_pair(Find(u), Find(v))] = true;
      con[make_pair(Find(v), Find(u))] = true;
    }
  }
}
bool DfsCheck(int x) {
  vis[x] = true;
  int ed = last[x], dr;
  bool flag = true;
  while (ed != -1) {
    dr = other[ed];
    if (col[dr] == -1) {
      col[dr] = 1 - col[x];
      flag &= DfsCheck(dr);
    } else if (col[dr] == col[x])
      return false;
    ed = pre[ed];
  }
  return flag;
}
void Solve() {
  int cnt = 0;
  if (!GG) {
    memset(vis, 0, sizeof(vis));
    memset(col, -1, sizeof(col));
    for (int i = 1; i <= n; i++)
      if (!vis[Find(i)]) {
        col[Find(i)] = 0;
        if (!DfsCheck(Find(i))) {
          GG = true;
          break;
        }
        cnt++;
      }
  }
  if (GG)
    printf("0\n");
  else {
    int ans = 1;
    for (int i = 1; i <= cnt - 1; i++) ans = (ans * 2) % Pt;
    printf("%d\n", ans);
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
  return 0;
}
