#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000 * 1000 * 1000 + 7;
const int maxn = 100000 + 100;
int n, m;
struct Node {
  int a, b;
  Node(int a = 0, int b = 0) : a(a), b(b) {}
};
vector<Node> V1, V2;
int fa[maxn];
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
vector<int> G[maxn];
int color[maxn];
bool dfs(int u, int c) {
  color[u] = c;
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (color[v] == -1) {
      if (!dfs(v, c ^ 1)) return false;
    } else {
      if (color[u] == color[v]) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (c)
      V1.push_back(Node(a, b));
    else
      V2.push_back(Node(a, b));
  }
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 0; i < V1.size(); ++i) {
    int x = V1[i].a, y = V1[i].b;
    int a = findfa(x), b = findfa(y);
    if (a != b) {
      fa[a] = b;
    }
  }
  for (int i = 0; i < V2.size(); ++i) {
    int x = V2[i].a, y = V2[i].b;
    int a = findfa(x), b = findfa(y);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  bool flag = true;
  memset(color, -1, sizeof(color));
  for (int i = 1; i <= n; ++i)
    if (color[i] == -1) {
      if (!dfs(i, 0)) {
        flag = false;
        break;
      }
    }
  if (!flag)
    puts("0");
  else {
    for (int i = 0; i < V2.size(); ++i) {
      int x = V2[i].a, y = V2[i].b;
      int a = findfa(x), b = findfa(y);
      if (a != b) fa[a] = b;
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
      if (findfa(i) == i) cnt++;
    long long res = 1;
    for (int i = 0; i < cnt - 1; ++i) res = res * 2 % mod;
    printf("%lld\n", res);
  }
  return 0;
}
