#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 3e5 + 5;
const int oo = 1e9 + 7;
vector<pair<int, int> > g[N];
vector<pair<int, int> > child[N];
int niche[N][2];
int upore[N][2];
bool vis[N];
int dfs(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second) sz += dfs(vw.first, u);
  }
  return sz;
}
int dfs2(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second == 0) sz += dfs2(vw.first, u);
  }
  return sz;
}
void dfs3(int u, int p = -1) {
  for (pair<int, int> vw : g[u])
    if (vw.first != p) {
      int v = vw.first;
      int w = vw.second;
      child[u].push_back(vw);
      dfs3(v, u);
      niche[u][w] += niche[v][w] + 1;
    }
}
void dfs4(int u) {
  if (child[u].size()) {
    int n = child[u].size();
    vector<vector<int> > pre(n, vector<int>(2, 0));
    vector<vector<int> > post(n, vector<int>(2, 0));
    int v = child[u].front().first;
    int w = child[u].front().second;
    pre[0][w] += niche[v][w] + 1;
    v = child[u].back().first;
    w = child[u].back().second;
    post[n - 1][w] += niche[v][w] + 1;
    for (int i = 1; i < n; i++) {
      pre[i][0] += pre[i - 1][0];
      pre[i][1] += pre[i - 1][1];
      pre[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = n - 2; i >= 0; i--) {
      post[i][0] += post[i + 1][0];
      post[i][1] += post[i + 1][1];
      post[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = 0; i < n; i++) {
      pair<int, int> vw = child[u][i];
      int v = vw.first;
      int w = vw.second;
      upore[v][w] += upore[u][w] + 1;
      if (i > 0) upore[v][w] += pre[i - 1][w];
      if (i < n - 1) upore[v][w] += post[i + 1][w];
      dfs4(v);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs2(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  dfs3(1);
  dfs4(1);
  for (int i = 1; i <= n; i++) {
    long long a = upore[i][0] + niche[i][0];
    long long b = upore[i][1] + niche[i][1];
    ans += a * b;
  }
  cout << ans << "\n";
}

### Prompt

Develop a solution in CPP to the problem described below:

You are given a tree (an undirected connected acyclic graph) consisting of n vertices and n - 1 edges. A number is written on each edge, each number is either 0 (let's call such edges 0-edges) or 1 (those are 1-edges).

Let's call an ordered pair of vertices (x, y) (x ≠ y) valid if, while traversing the simple path from x to y, we never go through a 0-edge after going through a 1-edge. Your task is to calculate the number of valid pairs in the tree.

Input

The first line contains one integer n (2 ≤ n ≤ 200000) — the number of vertices in the tree.

Then n - 1 lines follow, each denoting an edge of the tree. Each edge is represented by three integers x_i, y_i and c_i (1 ≤ x_i, y_i ≤ n, 0 ≤ c_i ≤ 1, x_i ≠ y_i) — the vertices connected by this edge and the number written on it, respectively.

It is guaranteed that the given edges form a tree.

Output

Print one integer — the number of valid pairs of vertices.

Example

Input


7
2 1 1
3 2 0
4 2 1
5 2 0
6 7 1
7 2 1


Output


34

Note

The picture corresponding to the first example:

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 3e5 + 5;
const int oo = 1e9 + 7;
vector<pair<int, int> > g[N];
vector<pair<int, int> > child[N];
int niche[N][2];
int upore[N][2];
bool vis[N];
int dfs(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second) sz += dfs(vw.first, u);
  }
  return sz;
}
int dfs2(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second == 0) sz += dfs2(vw.first, u);
  }
  return sz;
}
void dfs3(int u, int p = -1) {
  for (pair<int, int> vw : g[u])
    if (vw.first != p) {
      int v = vw.first;
      int w = vw.second;
      child[u].push_back(vw);
      dfs3(v, u);
      niche[u][w] += niche[v][w] + 1;
    }
}
void dfs4(int u) {
  if (child[u].size()) {
    int n = child[u].size();
    vector<vector<int> > pre(n, vector<int>(2, 0));
    vector<vector<int> > post(n, vector<int>(2, 0));
    int v = child[u].front().first;
    int w = child[u].front().second;
    pre[0][w] += niche[v][w] + 1;
    v = child[u].back().first;
    w = child[u].back().second;
    post[n - 1][w] += niche[v][w] + 1;
    for (int i = 1; i < n; i++) {
      pre[i][0] += pre[i - 1][0];
      pre[i][1] += pre[i - 1][1];
      pre[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = n - 2; i >= 0; i--) {
      post[i][0] += post[i + 1][0];
      post[i][1] += post[i + 1][1];
      post[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = 0; i < n; i++) {
      pair<int, int> vw = child[u][i];
      int v = vw.first;
      int w = vw.second;
      upore[v][w] += upore[u][w] + 1;
      if (i > 0) upore[v][w] += pre[i - 1][w];
      if (i < n - 1) upore[v][w] += post[i + 1][w];
      dfs4(v);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs2(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  dfs3(1);
  dfs4(1);
  for (int i = 1; i <= n; i++) {
    long long a = upore[i][0] + niche[i][0];
    long long b = upore[i][1] + niche[i][1];
    ans += a * b;
  }
  cout << ans << "\n";
}

```