#include using namespace std; using ll = long long; template using V = vector; using VI = V; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define all(a) a.begin(),a.end() inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template inline istream& operator>>(istream& is, V& v) { for (auto& a : v)is >> a; return is; } template class weighted_graph { bool directed, zero_indexed_input; int n, m; V>> g; public: weighted_graph(int n, int m, bool directed = false, bool zero_indexed_input = false) : n(n), m(m), directed(directed), zero_indexed_input(zero_indexed_input), g(n) {}; friend istream& operator>>(istream& is, weighted_graph& me) { FOR(i, 0, me.m) { int a, b, c; is >> a >> b >> c; if (!me.zero_indexed_input)--a, --b; me.g[a].emplace_back(b, c); if (!me.directed)me.g[b].emplace_back(a, c); } return is; } const V>& operator[](int i) { return g[i]; } int size() { return n; } }; void compress(VI& x) { VI y = x; sort(all(y)); y.erase(unique(all(y)), y.end()); for (int& a : x)a = lower_bound(all(y), a) - y.begin(); } int main() { init(); int n; cin >> n; weighted_graph g(n, n - 1); cin >> g; VI x(n), sz(n, 1); auto dfs1 = [&](auto&& f, int cur, int par)->int { for (auto [to, weight] : g[cur]) { if (to == par)continue; x[to] = x[cur] ^ weight; sz[cur] += f(f, to, cur); } return sz[cur]; }; dfs1(dfs1, 0, -1); compress(x); ll ans = n * ll(n - 1); VI y(n), cnt(n); auto dfs2 = [&](auto&& f, int cur, int par)->void { ans -= y[x[cur]]; ans -= ll(sz[cur]) * cnt[x[cur]]; for (auto [to, weight] : g[cur]) { if (to == par)continue; int y_tmp = y[x[cur]], cnt_tmp = cnt[x[cur]]; y[x[cur]] = n - sz[to], cnt[x[cur]] = 1; f(f, to, cur); y[x[cur]] = y_tmp, cnt[x[cur]] = cnt_tmp; } y[x[cur]] += sz[cur]; ++cnt[x[cur]]; }; dfs2(dfs2, 0, -1); cout << ans << "\n"; return 0; }