#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 100100;
int n, m;
int p[maxn];
int q[maxn];
int ty[maxn];
int id[maxn];
int vis[maxn];
vector<int> like[maxn];
vector<int> hate[maxn];
int si[maxn];
int tt[maxn];
int l, r;
int of[maxn];
int cnt;
long long ans;
void dfs(int v) {
  for (int i = 0; i < like[v].size(); i++) {
    int e = like[v][i];
    if (vis[e] == -1) {
      vis[e] = vis[v];
      dfs(e);
    }
    if (vis[v] != vis[e]) ans = 0;
  }
  for (int i = 0; i < hate[v].size(); i++) {
    int e = hate[v][i];
    if (vis[e] == -1) {
      vis[e] = !vis[v];
      dfs(e);
    }
    if (vis[v] == vis[e]) ans = 0;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, t;
    scanf("%d%d%d", &a, &b, &t);
    p[i] = a;
    q[i] = b;
    ty[i] = t;
    if (t == 1) {
      like[a].push_back(b);
      like[b].push_back(a);
    } else {
      hate[a].push_back(b);
      hate[b].push_back(a);
    }
  }
  memset(vis, -1, sizeof(vis));
  ans = (mod + 1) / 2;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      ans = ans * 2 % mod;
      vis[i] = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
  return 0;
}
