#include using namespace std; #define ll long long #define all(x) x.begin(),x.end() #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep_r(i,a,b) for(int i=a;i>=b;i--) #define each(a,x) for (auto& x : a) using pi = pair; using pl = pair; using vi = vector; using vl = vector; #define sz(x) int(x.size()) #define so(x) sort(all(x)) #define so_r(x) sort(all(x),greater()) #define lb lower_bound #define ub upper_bound const char nl = '\n'; int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int bit_cnt(int x){ return __builtin_popcount(x); } ll bex(ll a, ll b, ll mod = 1e9 + 7){ll res = 1LL; while(b){ if (b&1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;} template bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b fa,sz; void init(int n){ fa.resize(n+1); sz.resize(n+1); rep(i,1,n) fa[i] = i; rep(i,1,n) sz[i] = 1; } int find(int u) { return (u == fa[u] ? u : fa[u] = find(fa[u])); } void merge(int u, int v){ u = find(u); v = find(v); if (u == v) return; if (sz[u] < sz[v]) swap(u,v); fa[v] = u; sz[u] += sz[v]; } }; DSU dsu[31]; int n; ll calc(int j, int x){ return 1LL * x * (x - 1) / 2 % MOD * (1LL << j) % MOD; } void solve(){ cin >> n; rep(i,0,30) dsu[i].init(n); rep(i,1,n-1){ int a,b,c; cin >> a >> b >> c; for (int j = 0; j <= 30; j++){ if (c>>j&1){ dsu[j].merge(a,b); } } } ll ans = 0; rep(i,0,30){ set> st; rep(j,1,n){ int fa = dsu[i].find(j); st.insert({fa,dsu[i].sz[fa]}); } each(st,p){ int comp_size = p.second; ans += calc(i, comp_size); ans %= MOD; } } cout << ans << nl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--){ solve(); } }