#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include using namespace std; using ll = long long; ll dfs(int N, vector &a, vector &b,vector &c, int &d, ll &P) { ll res = 0; set visited; deque que; vector> edge(N,vector()); for( int i = 0; i < N-1; i++ ) { if( (c[i]>>d)&1 ) { edge[a[i]].push_back(b[i]); edge[b[i]].push_back(a[i]); } } for( int i = 0; i < N; i++ ) { if( visited.count(i) ) continue; que.push_back(i); ll cnt = 0; while( !que.empty() ) { int now = que.front(); que.pop_front(); if( visited.count(now) ) continue; visited.insert(now); cnt++; for( auto& next : edge[now] ) { if( !visited.count(next) ) que.push_back(next); } } res += cnt*(cnt-1)/2%P; res %= P; } return res; } void solve() { int N; cin >> N; vector a(N-1); vector b(N-1); vector c(N-1); ll ans = 0; ll pt = 1; ll P = 1000000007; for( int i = 0; i < N-1; i++ ) { cin >> a[i]>> b[i] >> c[i]; a[i]--; b[i]--; } for( int i = 0; i < 30; i++, pt*=2 ) { ans += dfs(N,a,b,c,i,P)*pt%P; ans %= P; } cout << ans << endl; } int main() { int t = 1; while( t > 0 ) { solve(); t--; } }