#include <bits/stdc++.h>
using namespace std;
bool red[100002];
bool blue[100002];
int belong[100002];
inline int root(int b) {
  if (belong[b] == -1) {
    return b;
  }
  belong[b] = root(belong[b]);
  return belong[b];
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) {
    return;
  }
  belong[a] = b;
}
int n;
int m;
vector<pair<int, int> > dif;
vector<int> v[100002];
bool used[100002];
vector<int> ord;
bool re[100002];
bool bl[100002];
bool dc[100002];
bool cl[100002];
bool once = false;
bool ll[100002];
bool rr[100002];
inline void dfs(int b, bool r = false, bool l = false) {
  rr[b] = r;
  ll[b] = l;
  r |= re[b];
  l |= bl[b];
  if (r && l) {
    puts("0");
    exit(0);
  }
  if (l || r) {
    once = true;
  }
  dc[b] = true;
  for (int i = 0; i < v[b].size(); i++) {
    int go = v[b][i];
    if (dc[go]) {
      if (cl[go] == cl[b]) {
        puts("0");
        exit(0);
      }
      if (ll[go] == r && rr[go] == l) {
        continue;
      }
      dfs(go, l, r);
      continue;
    } else {
      cl[go] = cl[b] ^ 1;
      dfs(go, l, r);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  memset(belong, -1, sizeof(belong));
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a > b) {
      swap(a, b);
    }
    a--;
    b--;
    if (a == 0) {
      if (c == 0) {
        red[b] = true;
      } else {
        blue[b] = true;
      }
      if (used[b] == false) {
        used[b] = true;
        ord.push_back(b);
      }
      continue;
    }
    if (c == 0) {
      dif.push_back(make_pair(a, b));
    } else {
      merge(a, b);
    }
  }
  for (int i = 0; i < dif.size(); i++) {
    if (root(dif[i].first) == root(dif[i].second)) {
      puts("0");
      return 0;
    }
    v[root(dif[i].first)].push_back(root(dif[i].second));
    v[root(dif[i].second)].push_back(root(dif[i].first));
  }
  for (int i = 1; i < n; i++) {
    if (used[i] == false) {
      ord.push_back(i);
    }
  }
  for (int i = 1; i < n; i++) {
    int k = root(i);
    re[k] |= red[i];
    bl[k] |= blue[i];
  }
  long long int ans = 1;
  for (int i = 0; i < ord.size(); i++) {
    int ii = i;
    i = ord[i];
    int k = root(i);
    if (dc[k] == false) {
      once = false;
      dfs(k, re[i], bl[i]);
      if (once == false) {
        ans *= 2LL;
        ans %= 1000000007LL;
      }
    }
    i = ii;
  }
  printf("%lld\n", ans);
  return 0;
}
