#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 5;
const int M = 1005;
const int K = 26;
const int inf = (int)1e9 + 7;
vector<int> g[N][2];
bool u[N];
int c[N];
int comp[N];
int s;
bool check(int v, int k) {
  c[v] = k;
  u[v] = 1;
  for (int w = 0; w < 2; ++w) {
    for (int i = 0; i < (int)g[v][w].size(); ++i) {
      int to = g[v][w][i];
      if (c[to] == c[v] && w == 0) {
        return 0;
      }
      if (c[to] != c[v] && w == 1 && c[to] != -1) {
        return 0;
      }
      if (!u[to] && !check(to, (w == 0 ? 1 - k : k))) {
        return 0;
      }
    }
  }
  return 1;
}
void dfs(int v) {
  u[v] = 1;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < (int)g[v][i].size(); ++j) {
      int to = g[v][i][j];
      if (!u[to]) {
        dfs(to);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a, --b;
    g[a][c].push_back(b);
    g[b][c].push_back(a);
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (!u[i]) {
      dfs(i);
      ++cnt;
    }
  }
  memset(c, -1, sizeof c);
  memset(u, 0, sizeof u);
  for (int i = 0; i < n; ++i) {
    if (!u[i] && !check(i, 0)) {
      puts("0");
      return 0;
    }
  }
  int ans = 1;
  for (int i = 1; i < cnt; ++i) {
    ans *= 2;
    ans %= inf;
  }
  printf("%d", ans);
  return 0;
}
