#include <bits/stdc++.h>
using namespace std;
inline int getint() {
  int r = 1, v = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') r = -1;
  for (; isdigit(ch); ch = getchar()) v = v * 10 - '0' + ch;
  return r * v;
}
const int N = 100010, P = 1000000007;
int head[N], nxt[N << 1], to[N << 1], cnt;
void add(int x, int y) {
  to[++cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
  to[++cnt] = x;
  nxt[cnt] = head[y];
  head[y] = cnt;
}
int n, m;
int fa[N], color[N];
inline int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
struct ques {
  int x, y, z;
} q[N];
bool operator<(ques a, ques b) { return a.z > b.z; }
bool flag, v[N];
void dfs(int x, int cl) {
  v[x] = 1;
  color[x] = cl;
  for (int i = head[x]; i; i = nxt[i])
    if (!v[to[i]])
      dfs(to[i], 1 - cl);
    else if (color[to[i]] != 1 - cl)
      flag = 1;
}
int main() {
  n = getint();
  m = getint();
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i)
    q[i].x = getint(), q[i].y = getint(), q[i].z = getint();
  sort(q + 1, q + m + 1);
  for (int i = 1; i <= m; ++i) {
    int f1 = getf(q[i].x), f2 = getf(q[i].y);
    if (q[i].z) {
      if (f1 != f2) fa[f1] = f2;
    } else
      add(f1, f2);
  }
  long long ans = 1;
  for (int i = 1; i <= n; ++i)
    if (getf(i) == i && !v[i]) {
      ans = ans * 2 % P;
      dfs(i, 0);
    }
  ans = ans * 500000004 % P;
  if (flag)
    puts("0");
  else
    cout << ans << endl;
  return 0;
}
