#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int mod = 1000000007;
struct edge {
  int to, len, next;
};
int head[N], color[N];
edge e[2 * N];
int read() {
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9';) ch = getchar();
  int d = 0;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) d = d * 10 + ch - 48;
  return d;
}
void push(int k, int x, int y, int z) {
  e[k].to = y;
  e[k].len = z;
  e[k].next = head[x];
  head[x] = k;
}
bool dfs(int k) {
  int p = head[k];
  while (p) {
    if (color[e[p].to] == 2) {
      color[e[p].to] = color[k] ^ e[p].len;
      if (!dfs(e[p].to)) return 0;
    } else if (color[e[p].to] != (color[k] ^ e[p].len))
      return 0;
    p = e[p].next;
  }
  return 1;
}
int main() {
  int n = read(), m = read();
  memset(head, 0, sizeof(head));
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read(), z = read();
    z ^= 1;
    push(2 * i - 1, x, y, z);
    push(2 * i, y, x, z);
  }
  int ans = (mod + 1) >> 1;
  for (int i = 1; i <= n; i++) color[i] = 2;
  for (int i = 1; i <= n; i++)
    if (color[i] == 2) {
      ans = ans * 2 % mod;
      color[i] = 0;
      if (!dfs(i)) {
        ans = 0;
        break;
      }
    }
  printf("%d\n", ans);
  return 0;
}
