#include <bits/stdc++.h>
struct ele {
  int y, st;
} tmp;
std::vector<ele> edge[100100];
std::vector<int> gr[100100];
bool vi[100100];
int co[100100], N;
int flag, sp[100100];
void dfs(int x) {
  int c, t;
  for (int k = 0; k < edge[x].size(); k++) {
    c = edge[x][k].y;
    t = edge[x][k].st;
    if (c == 1) continue;
    if (!vi[c]) {
      vi[c] = true;
      if (t == 0)
        co[c] = 1 + co[x] % 2;
      else
        co[c] = co[x];
      gr[N].push_back(c);
      dfs(c);
    } else {
      if ((co[x] == co[c] && t == 0) || (co[x] != co[c] && t == 1)) {
        flag = 0;
        return;
      }
    }
  }
  return;
}
int main() {
  int k, n, m, t, a, b, c, d, i;
  scanf("%d%d", &n, &m);
  flag = 1;
  memset(sp, -1, sizeof(sp));
  for (k = 1; k <= m; k++) {
    scanf("%d%d%d", &a, &b, &t);
    edge[a].push_back(ele{b, t});
    edge[b].push_back(ele{a, t});
    if (a == 1) sp[b] = t + 1;
    if (b == 1) sp[a] = t + 1;
  }
  for (k = 2; k <= n; k++) {
    if (!vi[k]) {
      N++;
      gr[N].push_back(k);
      vi[k] = 1;
      co[k] = 1;
      dfs(k);
    }
  }
  long long ans = 1, MOD = 1000000007, tm, s;
  if (flag == 0) {
    printf("0\n");
    return 0;
  }
  for (k = 1; k <= N; k++) {
    tm = 2;
    s = -1;
    for (i = 0; i < gr[k].size(); i++) {
      c = gr[k][i];
      if (sp[c] == -1) continue;
      if (s == -1) {
        if (sp[c] == co[c])
          s = 0;
        else
          s = 1;
        tm = 1;
      } else {
        if (sp[c] == co[c] && s == 1) tm = 0;
        if (sp[c] != co[c] && s == 0) tm = 0;
      }
    }
    ans *= tm;
    ans %= MOD;
  }
  printf("%I64d", ans);
}
