#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int BASE = 1e9 + 7;
int n, m, t[N], ans;
vector<pair<int, int> > g[N];
void dfs(int x) {
  for (int i = 0; i < g[x].size(); i++) {
    int y = g[x][i].first;
    int z = g[x][i].second ^ t[x];
    if (t[y] < 0) {
      t[y] = z;
      dfs(y);
    }
    if (t[y] != z) ans = 0;
  }
}
void add(int &x, int y) {
  x += y;
  if (x >= BASE) x -= BASE;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    --x;
    --y;
    z ^= 1;
    g[x].push_back(make_pair(y, z));
    g[y].push_back(make_pair(x, z));
  }
  memset(t, -1, sizeof(t));
  ans = (BASE + 1) / 2;
  for (int i = 0; i < n; i++) {
    if (t[i] < 0) {
      add(ans, ans);
      t[i] = 0;
      dfs(i);
    }
  }
  printf("%d\n", ans);
}
