#include <bits/stdc++.h>
const int MAX = 100005;
const long long int MOD = 1000000007;
std::vector<int> v[MAX];
int rem[MAX][2];
int num[MAX];
void dfs(int x, int l) {
  if (num[x] != 0) return;
  num[x] = l;
  for (int a : v[x]) dfs(a, l);
}
long long int pot(long long int a, int n) {
  if (n == 0) return 1LL;
  if (n & 1) return (a * pot(a, n - 1)) % MOD;
  return pot((a * a) % MOD, n / 2);
}
struct par {
  int a;
  int b;
};
bool operator<(par a, par b) {
  if (a.a == b.a) return a.b < b.b;
  return a.a < b.a;
}
std::vector<int> fin[MAX];
int id[MAX];
bool dfs2(int x, int l) {
  if (id[x] == 3 - l) return 0;
  if (id[x] == l) return 1;
  id[x] = l;
  for (int a : fin[x]) {
    if (!dfs2(a, 3 - l)) return 0;
  }
  return 1;
}
int main() {
  int n, m, a, b, c, l = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    if (c == 1) {
      v[a].push_back(b);
      v[b].push_back(a);
    }
    if (c == 0) {
      rem[l][0] = a;
      rem[l][1] = b;
      l++;
    }
  }
  int lic = 0;
  for (int i = 1; i <= n; i++) {
    if (num[i] == 0) {
      lic++;
      dfs(i, lic);
    }
  }
  for (int i = 0; i < l; i++) {
    a = num[rem[i][0]];
    b = num[rem[i][1]];
    fin[a].push_back(b);
    fin[b].push_back(a);
  }
  int tmp = 0;
  for (int i = 1; i <= lic; i++) {
    if (id[i] == 0) {
      if (!dfs2(i, 1)) {
        printf("0\n");
        return 0;
      }
      tmp++;
    }
  }
  printf("%d\n", (int)pot(2LL, tmp - 1));
  return 0;
}
