#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; int main(){ int N; cin >> N; vector<vector<pair<int, int>>> E(N); for (int i = 0; i < N - 1; i++){ int A, B, C; cin >> A >> B >> C; A--; B--; E[A].push_back(make_pair(C, B)); E[B].push_back(make_pair(C, A)); } long long ans = 0; for (int i = 0; i < 30; i++){ vector<bool> used(N, false); for (int j = 0; j < N; j++){ if (!used[j]){ used[j] = true; int cnt = 0; queue<int> Q; Q.push(j); while (!Q.empty()){ int v = Q.front(); Q.pop(); cnt++; for (auto P : E[v]){ if ((P.first >> i & 1) == 1){ int w = P.second; if (!used[w]){ used[w] = true; Q.push(w); } } } } ans += (long long) cnt * (cnt - 1) / 2 % MOD * (1 << i) % MOD; } } } ans %= MOD; cout << ans << endl; }