#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, m, x, y, type, h[N];
vector<pair<int, int> > v[N];
queue<int> q;
int main() {
  cin >> n >> m;
  for (auto i = 1; i <= m; i++) {
    scanf("%d%d%d", &x, &y, &type);
    v[x].push_back(pair<int, int>(type ^ 1, y));
    v[y].push_back(pair<int, int>(type ^ 1, x));
  }
  int ans = 1;
  memset(h, -1, sizeof(h));
  for (auto i = 1; i <= n; i++)
    if (h[i] == -1) {
      h[i] = 0;
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (auto j : v[x]) {
          int y = j.second, ns = h[x] ^ j.first;
          if (h[y] == -1) {
            h[y] = ns;
            q.push(y);
          } else if (h[y] != ns) {
            printf("0");
            return 0;
          }
        }
      }
      if (i != 1) ans = ans * 2 % MOD;
    }
  cout << ans;
}
