#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[100000];
vector<long long> graph[100000];
int visited[100000], flag;
void dfs(long long start, int group) {
  int i, temp;
  if (visited[start] != -1) {
    if (visited[start] != group) flag = 1;
    return;
  }
  visited[start] = group;
  for (i = 0; i < adj[start].size(); i++) {
    if (adj[start][i].second != 1)
      temp = (group == 1 ? 0 : 1);
    else
      temp = group;
    dfs(adj[start][i].first, temp);
  }
}
int main() {
  long long i, m, s1, s2, val, cnt, res;
  int n;
  while (cin >> n >> m) {
    for (i = 0; i < n; i++) {
      adj[i].clear();
      graph[i].clear();
    }
    memset(visited, -1, n * sizeof(int));
    cnt = 0;
    res = 1;
    for (i = 0; i < m; i++) {
      cin >> s1 >> s2 >> val;
      s1--;
      s2--;
      adj[s1].push_back(make_pair(s2, val));
      adj[s2].push_back(make_pair(s1, val));
    }
    for (i = 0; i < n; i++) {
      if (visited[i] == -1) {
        flag = 0;
        dfs(i, 1);
        cnt++;
      }
      if (flag) break;
    }
    if (flag) {
      printf("0\n");
      continue;
    }
    for (i = 0; i < cnt - 1; i++) {
      res *= 2;
      res %= 1000000007;
    }
    cout << res << endl;
  }
  return 0;
}
