#include using namespace std; using ll = long long; using Vi = vector; using VVi = vector; using Vl = vector; using VVl = vector; void tprsort(VVi& gr, Vi& tpr, Vi& par, int s) { for (auto v : gr[s]) { if (v == par[s]) { continue; } par[v] = s; tprsort(gr, tpr, par, v); } tpr.push_back(s); } int main () { ll m = 1e9 + 7; ll f2 = (m + 1) / 2; int N; cin >> N; VVi tree(N); VVl dist(N); for (int i = 1; i < N; i ++) { int a, b; ll c; cin >> a >> b >> c; a --; b --; tree[a].push_back(b); tree[b].push_back(a); dist[a].push_back(c); dist[b].push_back(c); } Vi tpr, par(N, -1); tprsort(tree, tpr, par, 0); ll ans = 0; for (int i_ = 0; i_ < 30; i_ ++) { Vl dp(N); ll kj = 0; for (auto u : tpr) { dp[u] = 1; ll sqsum = 1; for (int i = 0; i < tree[u].size(); i ++) { int v = tree[u][i]; ll c = dist[u][i]; if (v == par[u]) { continue; } if ((c >> i_) & 1) { dp[u] += dp[v]; sqsum += (dp[v] * dp[v]) % m; sqsum %= m; } } kj += ((((dp[u] * dp[u]) % m - sqsum + m) % m) * f2) % m; kj %= m; } ans += (kj * ((1 << i_) % m)) % m; ans %= m; } cout << ans << endl; }