#include <bits/stdc++.h>
using namespace std;
int Num;
char CH[20];
const int inf = 0x3f3f3f3f;
inline long long read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
vector<pair<int, int> > e[2000001];
int vis[2000001];
int ans;
void dfs(int x) {
  for (int i = 0; i < e[x].size(); i++) {
    int v = e[x][i].first;
    int t = e[x][i].second;
    if (vis[v] == -1) {
      if (t == 1)
        vis[v] = vis[x];
      else
        vis[v] = 1 - vis[x];
      dfs(v);
    }
    if (t == 1) {
      if (vis[v] != vis[x]) ans = 0;
    } else {
      if (vis[v] == vis[x]) ans = 0;
    }
  }
}
int main() {
  int n = read(), m = read();
  for (int i = 0; i < m; i++) {
    int a = read(), b = read(), c = read();
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  ans = (1000000007 + 1) / 2;
  memset(vis, -1, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) {
      ans = (ans + ans) % 1000000007;
      vis[i] = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
}
