#include #include #include #include using namespace std; int main() { int N; cin >> N; vector A(N - 1), B(N - 1), C(N - 1); for (int i = 0; i < N - 1; i++) { cin >> A[i] >> B[i] >> C[i]; A[i] -= 1; B[i] -= 1; } const int MOD = 1000000007; int answer = 0; for (int i = 0; i < 30; i++) { vector > G(N); for (int j = 0; j < N - 1; j++) { if ((C[j] >> i) & 1) { G[A[j]].push_back(B[j]); G[B[j]].push_back(A[j]); } } vector vis(N, false); function dfs = [&](int pos) { int res = 1; vis[pos] = true; for (int j : G[pos]) { if (!vis[j]) { res += dfs(j); } } return res; }; for (int j = 0; j < N; j++) { if (!vis[j]) { int res = dfs(j); long long combs = (1LL * res * (res - 1) / 2) % MOD; answer = (answer + (combs << i)) % MOD; } } } cout << answer << endl; return 0; }