#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
class node {
 public:
  vector<int> near;
};
node a[N];
node b[N];
int cmp[N];
bool df[N] = {0};
vector<int> tmp;
void dfs(int j) {
  tmp.push_back(j);
  df[j] = 1;
  vector<int> v = b[j].near;
  for (int i = 0; i < v.size(); i++) {
    if (!df[v[i]]) dfs(v[i]);
  }
}
int clr[N];
bool dfs1(int j) {
  df[j] = 1;
  int x = clr[j];
  vector<int> v = a[j].near;
  bool psbl = 1;
  for (int i = 0; i < v.size(); i++) {
    if (df[v[i]]) {
      if (clr[v[i]] + x != 1) return 0;
    } else {
      clr[v[i]] = 1 - x;
      psbl &= dfs1(v[i]);
    }
  }
  return psbl;
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  vector<pair<int, int> > edges;
  for (int i = 1; i <= m; i++) {
    int c;
    scanf("%d %d %d", &x, &y, &c);
    if (c == 1) {
      b[x].near.push_back(y);
      b[y].near.push_back(x);
    } else
      edges.push_back(pair<int, int>(x, y));
  }
  int cnt = 1;
  for (int i = 1; i <= n; i++) {
    if (df[i]) continue;
    dfs(i);
    for (int j = 0; j < tmp.size(); j++) cmp[tmp[j]] = cnt;
    cnt++;
    tmp.clear();
  }
  cnt--;
  for (int i = 0; i < edges.size(); i++) {
    x = edges[i].first;
    y = edges[i].second;
    x = cmp[x];
    y = cmp[y];
    a[x].near.push_back(y);
    a[y].near.push_back(x);
  }
  for (int i = 1; i <= cnt; i++) df[i] = 0;
  int cps = 0;
  for (int i = 1; i <= cnt; i++) {
    if (df[i]) continue;
    clr[i] = 0;
    bool psbl = dfs1(i);
    if (!psbl) {
      cout << 0 << endl;
      return 0;
    }
    cps++;
  }
  long long int res = 1;
  long long int mp = 1000000007;
  for (int i = 1; i < cps; i++) res = (res + res) % mp;
  cout << res << endl;
}
