#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int MOD = 1e9 + 7;
vector<pair<int, int> > G[maxn];
int vis[maxn];
int dp[maxn];
int dfs(int x, int v) {
  vis[x] = 1;
  dp[x] = v;
  for (pair<int, int> e : G[x]) {
    int a = e.first;
    int b = 1 - e.second;
    if (vis[a]) {
      if (dp[a] != (dp[x] ^ b)) return 0;
    } else {
      if (!dfs(a, v ^ b)) return 0;
    }
  }
  return 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    G[x].push_back(make_pair(y, v));
    G[y].push_back(make_pair(x, v));
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      if (!dfs(i, 0)) {
        puts("0");
        return 0;
      } else {
        if (!ans)
          ans = 1;
        else
          ans = ans * 2 % MOD;
      }
    }
  }
  cout << ans << endl;
}
