#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return a * b / gcd(a, b);
}
long long int fexp(long long int a, long long int b) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % 1000000007;
    b /= 2;
    a = a * a % 1000000007;
  }
  return ans;
}
long long int inverse(long long int a, long long int p) {
  return fexp(a, p - 2);
}
void ingraph(vector<vector<long long int> >& graph, long long int m) {
  long long int x, y;
  for (long long int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
}
long long int modify(long long int n) {
  long long int res = n;
  res %= 1000000007;
  res += 1000000007;
  res %= 1000000007;
  return res;
}
struct vertex {
  long long int parent;
  long long int rank;
  long long int comp;
  vector<long long int> vec;
};
struct vertex ver[2000005];
long long int find(long long int n) {
  long long int x = n;
  while (x != ver[x].parent) {
    x = ver[x].parent;
  }
  long long int y = n;
  long long int z = ver[y].parent;
  while (y != z) {
    ver[y].parent = x;
    y = z;
    z = ver[z].parent;
  }
  return x;
}
void makeset(long long int n) {
  ver[n].rank = 0;
  ver[n].parent = n;
  ver[n].comp = 1;
  ver[n].vec.push_back(n);
}
void initiate(long long int n) {
  for (long long int i = 0; i < n; i++) makeset(i);
}
void join(vector<long long int>& x, vector<long long int>& y) {
  x.insert(x.end(), y.begin(), y.end());
}
void uni(long long int x, long long int y) {
  long long int px = find(x);
  long long int py = find(y);
  if (px == py)
    return;
  else if (ver[px].rank > ver[py].rank) {
    ver[py].parent = px;
    ver[px].comp += ver[py].comp;
    join(ver[px].vec, ver[py].vec);
  } else if (ver[py].rank > ver[px].rank) {
    ver[px].parent = py;
    ver[py].comp += ver[px].comp;
    join(ver[py].vec, ver[px].vec);
  } else {
    ver[py].parent = px;
    ver[px].rank++;
    ver[px].comp += ver[py].comp;
    join(ver[px].vec, ver[py].vec);
  }
}
bool visited[1000001] = {0};
vector<long long int> col(1000001, -1);
bool bip(vector<vector<long long int> >& graph, long long int v,
         long long int clr) {
  visited[v] = 1;
  col[v] = clr;
  bool ans = 1;
  for (long long int u : graph[v]) {
    if (!visited[u])
      ans = ans && bip(graph, u, !clr);
    else if (col[u] == col[v])
      return 0;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  t = 1;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    vector<pair<long long int, long long int> > ged;
    initiate(n);
    long long int u, v, w;
    for (long long int i = 0; i < m; i++) {
      cin >> u >> v >> w;
      --u, --v;
      if (w)
        uni(u, v);
      else {
        ged.push_back({u, v});
      }
    }
    vector<long long int> roots;
    for (long long int i = 0; i < n; i++) {
      if (find(i) == i) roots.push_back(i);
    }
    vector<vector<long long int> > graph(n);
    for (long long int i = 0; i < ged.size(); i++) {
      u = find(ged[i].first);
      v = find(ged[i].second);
      graph[u].push_back(v);
      graph[v].push_back(u);
    }
    long long int ans = inverse(2, 1000000007);
    for (long long int x : roots) {
      if (!visited[x]) {
        if (bip(graph, x, 1)) {
          ans *= 2;
          ans %= 1000000007;
        } else {
          ans = 0;
          break;
        }
      }
    }
    cout << ans;
  }
}
