#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const int mod = 1000000007;
int fa[maxn], block[maxn];
struct edge {
  int x, y;
  edge(int x, int y) {
    this->x = x;
    this->y = y;
  }
};
vector<int> G[maxn];
int color[maxn];
vector<edge> pic;
set<int> st;
int lisan[maxn], top = 0;
long long power(long long base, long long tim) {
  long long ret = 1;
  while (tim) {
    if (tim & 1) ret = ret * base % mod;
    base = base * base % mod;
    tim = tim >> 1;
  }
  return ret;
}
int getfather(int x) {
  if (fa[x] == x) return fa[x];
  fa[x] = getfather(fa[x]);
  return fa[x];
}
bool dfs(int x) {
  for (int i = 0; i < G[x].size(); i++) {
    int y = G[x][i];
    if (color[y] == color[x]) return false;
    if (color[y] + color[x] == 3) continue;
    color[y] = 3 - color[x];
    if (!dfs(y)) return false;
  }
  return true;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y, type;
    scanf("%d%d%d", &x, &y, &type);
    if (type == 1) {
      int f1 = getfather(x), f2 = getfather(y);
      if (f1 != f2) fa[f1] = f2;
    } else
      pic.push_back(edge(x, y));
  }
  for (int i = 1; i <= n; i++) {
    int f = getfather(i);
    if (st.count(f) == 0) {
      st.insert(f);
      top++;
      lisan[top] = f;
    }
  }
  sort(lisan + 1, lisan + top + 1);
  for (int i = 1; i <= n; i++) {
    int f = getfather(i);
    block[i] = lower_bound(lisan, lisan + top + 1, f) - lisan;
  }
  for (int i = 0; i < pic.size(); i++) {
    edge thi = pic[i];
    int b1 = block[thi.x], b2 = block[thi.y];
    if (b1 == b2) {
      printf("0\n");
      return 0;
    }
    G[b1].push_back(b2);
    G[b2].push_back(b1);
  }
  memset(color, 0, sizeof(color));
  int ans = 0;
  for (int i = 1; i <= top; i++)
    if (!color[i]) {
      ans++;
      color[i] = 1;
      if (!dfs(i)) {
        printf("0\n");
        return 0;
      }
    }
  cout << power(2, ans - 1) << endl;
  return 0;
}
