#include <bits/stdc++.h>
using namespace std;
class Graph {
 public:
  int n;
  int m;
  int num_components;
  vector<forward_list<int>> adj_info;
  vector<int> visited;
  vector<int> color;
  Graph(int n_) {
    n = n_;
    adj_info.resize(n + 1);
    m = 0;
    num_components = n;
  }
  void add(int i, int j) {
    adj_info[i].push_front(j);
    adj_info[j].push_front(i);
    m++;
  }
  void bfs() {
    visited.resize(n + 1, -1);
    queue<int> q;
    int c_no = 0;
    for (int i = 1; i <= n; i++) {
      if (visited[i] != -1) continue;
      c_no++;
      q.push(i);
      visited[i] = c_no;
      while (q.size() != 0) {
        int top = q.front();
        q.pop();
        for (int j : adj_info[top]) {
          if (visited[j] == -1) {
            visited[j] = c_no;
            q.push(j);
          }
        }
      }
    }
    num_components = c_no;
  }
  int color2() {
    color.resize(n + 1, -1);
    int ways = 0;
    for (int i = 1; i <= n; i++) {
      if (color[i] == -1) {
        ways++;
        color[i] = 0;
        queue<int> q;
        q.push(i);
        while (q.size() != 0) {
          int top = q.front();
          q.pop();
          for (int j : adj_info[top]) {
            if (color[j] == -1) {
              color[j] = !color[top];
              q.push(j);
            } else if (color[j] == color[top]) {
              return 0;
            }
          }
        }
      }
    }
    return ways;
  }
};
long long pow(int a, int b, int mod) {
  if (b < 0) {
    return 0;
  }
  if (b == 0) {
    return 1 % mod;
  }
  if (b == 1) {
    return a % mod;
  }
  long long ans = pow(a, b / 2, mod);
  ans = (ans * ans) % mod;
  if (b % 2 == 1) {
    ans = (ans * (a % mod)) % mod;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> hate;
  hate.reserve(m);
  Graph g(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 1) {
      g.add(a, b);
    } else {
      hate.push_back(make_pair(a, b));
    }
  }
  g.bfs();
  vector<int> heads;
  heads.reserve(n);
  for (int i = 0; i < g.num_components; i++) {
    heads.push_back(i);
  }
  Graph g2(g.num_components);
  bool ok = true;
  for (int i = 0; i < hate.size(); i++) {
    int a = g.visited[hate[i].first];
    int b = g.visited[hate[i].second];
    if (a == b) {
      ok = false;
      cout << 0 << endl;
      return 0;
    }
    g2.add(a, b);
  }
  cout << pow(2, g2.color2() - 1, 1000000007) << endl;
  return 0;
}
