#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct node {
  int v, x;
};
vector<node> g[100010];
int visit[100010], col[100010];
bool bfs(int i) {
  queue<int> qu;
  qu.push(i);
  visit[i] = 1;
  col[i] = 1;
  while (!qu.empty()) {
    int top = qu.front();
    qu.pop();
    for (int j = 0; j < g[top].size(); j++) {
      int v = g[top][j].v;
      int x = g[top][j].x;
      if (col[v] == -1) {
        if (x == 0)
          col[v] = !col[top];
        else
          col[v] = col[top];
      } else {
        if (x == 0 && col[v] == col[top]) return false;
        if (x == 1 && col[v] == !col[top]) return false;
      }
      if (!visit[v]) {
        qu.push(v);
      }
      visit[v] = 1;
    }
  }
  return true;
}
int main() {
  int n, m, a, b, k;
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &a, &b, &k);
      node tem;
      tem.x = k, tem.v = b;
      g[a].push_back(tem);
      tem.x = k, tem.v = a;
      g[b].push_back(tem);
    }
    memset(visit, 0, sizeof(visit));
    memset(col, -1, sizeof(col));
    long long ans = (mod + 1) / 2;
    for (int i = 1; i <= n; i++) {
      if (!visit[i]) {
        if (bfs(i))
          ans = (ans * 2) % mod;
        else {
          ans = 0;
          break;
        }
      }
    }
    printf("%lld\n", ans);
  }
}
