#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 10;
const int M = 1000 * 1000 * 1000 + 7;
int n, m;
vector<vector<int> > g;
vector<vector<int> > v;
vector<pair<int, int> > edg;
int a[N];
bool mark[N];
int cnt;
int C[N];
long long p = 1;
void dfs(int x, int t) {
  mark[x] = 1;
  a[x] = t;
  for (int i = 0; i < (((int)g[x].size())); ++i) {
    if (!mark[g[x][i]]) {
      dfs(g[x][i], t);
    }
  }
}
void dfs2(int x, int c) {
  mark[x] = 1;
  C[x] = c;
  for (int i = 0; i < (((int)v[x].size())); ++i) {
    if (!mark[v[x][i]]) {
      dfs2(v[x][i], 1 - c);
    }
  }
}
void dfs3(int x) {
  mark[x] = 1;
  for (int i = 0; i < (((int)g[x].size())); ++i) {
    if (!mark[g[x][i]]) {
      dfs3(g[x][i]);
    }
  }
}
int main() {
  cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; ++i) {
    int t1, t2, c;
    cin >> t1 >> t2 >> c;
    t1--;
    t2--;
    if (c) {
      g[t1].push_back(t2);
      g[t2].push_back(t1);
    } else {
      edg.push_back(make_pair(t1, t2));
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!mark[i]) {
      dfs(i, cnt);
      cnt++;
    }
  }
  for (int i = 0; i < (((int)edg.size())); ++i) {
    if (a[edg[i].first] == a[edg[i].second]) {
      cout << "0\n";
      return 0;
    }
  }
  v.resize(cnt);
  for (int i = 0; i < (((int)edg.size())); ++i) {
    v[a[edg[i].first]].push_back(a[edg[i].second]);
    v[a[edg[i].second]].push_back(a[edg[i].first]);
  }
  for (int i = 0; i < N; ++i) mark[i] = 0;
  for (int i = 0; i < cnt; ++i) {
    if (!mark[i]) dfs2(i, 0);
  }
  for (int i = 0; i < (((int)edg.size())); ++i) {
    if (C[a[edg[i].first]] == C[a[edg[i].second]]) {
      cout << "0\n";
      return 0;
    }
  }
  for (int i = 0; i < N; ++i) mark[i] = 0;
  for (int i = 0; i < (((int)edg.size())); ++i) {
    g[edg[i].first].push_back(edg[i].second);
    g[edg[i].second].push_back(edg[i].first);
  }
  for (int i = 0; i < n; ++i) {
    if (!mark[i]) {
      dfs3(i);
      if (i != 0) p = p * 2 % M;
    }
  }
  cout << p << "\n";
  return 0;
}
