#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int f[100010], f1[100010], n1 = 0, n2 = 0, cnt, top, p, s[100010], dfn[100010],
                           low[100010], be[100010], d[100010];
vector<int> g[100010];
int find(int a) { return a == f[a] ? a : f[a] = find(f[a]); }
int find1(int a) { return a == f1[a] ? a : f1[a] = find1(f1[a]); }
int find(vector<int> &v, int first) {
  int second = lower_bound(v.begin(), v.end(), first) - v.begin();
  if (second < v.size() && v[second] == first) return 1;
  return 0;
}
void tarjan(int u, int fa = 0) {
  d[u] = d[fa] + 1;
  low[u] = dfn[u] = ++cnt;
  s[++top] = u;
  for (int i = 0; i < g[u].size(); i++) {
    int j = g[u][i];
    if (j == fa) continue;
    if (!dfn[j]) {
      tarjan(j, u), low[u] = min(low[u], low[j]);
      if (find(g[fa], j)) {
        puts("0");
        exit(0);
      }
    } else {
      low[u] = min(low[u], dfn[j]);
      int len = d[u] - d[j] + 1;
      if (len & 1) {
        puts("0");
        exit(0);
      }
    }
  }
  if (dfn[u] == low[u]) {
    int j, k = 0;
    ++p;
    vector<int> v;
    do {
      j = s[top--];
      be[j] = p;
      k++;
    } while (j != u);
    if (k == 3) {
      puts("0");
      exit(0);
    }
  }
}
pair<int, int> a[100010], b[100010];
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * (long long)a % 1000000007;
    b >>= 1;
    a = 1LL * a * a % 1000000007;
  }
  return ans;
}
int main() {
  int T, i, j, k, ca = 0, n, m, t;
  scanf("%d%d", &n, &m);
  n1 = n2 = 0;
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &j, &k, &t);
    if (t == 1)
      a[n1++] = make_pair(j, k);
    else
      b[n2++] = make_pair(j, k);
  }
  for (i = 1; i <= n; i++) f[i] = i, f1[i] = i;
  for (i = 0; i < n1; i++) {
    j = a[i].first, k = a[i].second;
    int u = find(j), v = find(k);
    if (u != v) f[u] = v;
  }
  for (i = 0; i < n2; i++) {
    j = b[i].first, k = b[i].second;
    int u = find(j), v = find(k);
    if (u == v) {
      puts("0");
      return 0;
    }
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (i = 1; i <= n; i++)
    if (g[i].size()) {
      sort(g[i].begin(), g[i].end());
      g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
    }
  for (i = 1; i <= n; i++)
    if (!dfn[i] && find(i) == i) tarjan(i);
  for (i = 1; i <= n; i++)
    if (f[i] == i) {
      int first = be[i], u = find1(first);
      for (j = 0; j < g[i].size(); j++) {
        k = g[i][j];
        if (be[k] == first) continue;
        int second = be[k];
        int v = find1(second);
        if (u != v) f1[v] = u;
      }
    }
  for (i = 1, m = 0; i <= p; i++)
    if (f1[i] == i) {
      m++;
    }
  m--;
  int ans = pow(2, m);
  printf("%d\n", ans);
  return 0;
}
