#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 15;
const int Q = 1e9 + 7;
vector<int> g[M], gg[M], G[M];
vector<pair<int, int> > e;
int color[M], comp1[M];
bool used1[M], used2[M];
void fin() {
  cout << 0;
  exit(0);
}
void dfs1(int i, int c) {
  used1[i] = true;
  comp1[i] = c;
  for (int j = 0; j < (int)G[i].size(); j++)
    if (!used1[G[i][j]]) dfs1(G[i][j], c);
}
void dfs2(int i, int c) {
  used2[i] = true;
  color[i] = c;
  for (int j = 0; j < (int)gg[i].size(); j++) {
    int v = gg[i][j];
    if (!used2[v]) {
      dfs2(v, 1 - c);
    } else {
      if (color[v] == c) fin();
    }
  }
}
void dfs3(int i) {
  used1[i] = true;
  for (int j = 0; j < (int)g[i].size(); j++) {
    if (!used1[g[i][j]]) dfs3(g[i][j]);
  }
}
int main() {
  srand(time(NULL));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int s, f, c;
    scanf("%d%d%d", &s, &f, &c);
    if (c == 1) {
      G[s].push_back(f);
      G[f].push_back(s);
    } else
      e.push_back(make_pair(s, f));
    g[s].push_back(f);
    g[f].push_back(s);
  }
  int comp = 0;
  for (int i = 1; i <= n; i++)
    if (!used1[i]) {
      comp++;
      dfs1(i, comp);
    }
  for (int i = 0; i < (int)e.size(); i++) {
    int s = e[i].first, f = e[i].second;
    if (comp1[s] == comp1[f]) fin();
    gg[comp1[s]].push_back(comp1[f]);
    gg[comp1[f]].push_back(comp1[s]);
  }
  for (int i = 1; i <= comp; i++)
    if (!used2[i]) dfs2(i, 0);
  for (int i = 1; i <= n; i++) used1[i] = false;
  comp = 0;
  for (int i = 1; i <= n; i++)
    if (!used1[i]) {
      comp++;
      dfs3(i);
    }
  int ans = 1;
  for (int i = 0; i < comp - 1; i++) ans = (ans + ans) % Q;
  cout << ans;
  return 0;
}
