#include <bits/stdc++.h>
using namespace std;
bool flag;
int vis[(1 << 17)];
int n, m, k;
vector<pair<int, int> > vt[(1 << 17)];
void dfs(int u) {
  for (int i = 0; i < vt[u].size(); i++) {
    int v = vt[u][i].first;
    int c = vt[u][i].second;
    if (vis[v] == -1) {
      if (c == 1)
        vis[v] = vis[u];
      else
        vis[v] = !vis[u];
      dfs(v);
    }
    if (c == 1 && (vis[v] != vis[u])) flag = true;
    if (c == 0 && (vis[v] == vis[u])) flag = true;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    vt[u].push_back(make_pair(v, c));
    vt[v].push_back(make_pair(u, c));
  }
  memset(vis, -1, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      vis[i] = 0;
      dfs(i);
      k++;
    }
  }
  long long ans = 1;
  if (flag) {
    puts("0");
    return 0;
  }
  for (int i = 2; i <= k; i++) ans = ans * 2 % 1000000007;
  cout << ans << endl;
}
