#include <bits/stdc++.h>
using namespace std;
char BUFFER[100000 + 5];
bool readn(int &n) { return scanf("%d", &n) == 1; }
bool readl(long long &n) { return scanf("%I64d", &n) == 1; }
bool readd(double &n) { return scanf("%lf", &n) == 1; }
bool reads(string &s) {
  s = "";
  int n = scanf("%s", BUFFER);
  if (n == 1) s = BUFFER;
  return n == 1;
}
bool readln(string &s) {
  char *valid = gets(BUFFER);
  if (valid) s = BUFFER;
  return ((bool)valid);
}
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
vector<pair<int, int> > adj[maxn];
int vis[maxn];
bool dfs(int cur, int flag = 1) {
  if (vis[cur] == flag) return true;
  if (vis[cur] == -flag) return false;
  vis[cur] = flag;
  int i, newFlag;
  for (i = 0; i < ((int)adj[cur].size()); ++i) {
    if (adj[cur][i].second == 1)
      newFlag = flag;
    else
      newFlag = -flag;
    if (false == dfs(adj[cur][i].first, newFlag)) return false;
  }
  return true;
}
int main() {
  int i, n, m, a, b, c;
  for (i = 0; i < maxn; ++i) adj[i].clear();
  cin >> n >> m;
  while (m--) {
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  memset((vis), (0), sizeof(vis));
  long long ans = 1;
  for (i = 0; i < n; ++i)
    if (0 == vis[i]) {
      if (false == dfs(i)) {
        ans = 0;
        break;
      }
      if (i > 0) ans = (ans * 2) % mod;
    }
  cout << ans << endl;
  return 0;
}
