#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int main() { constexpr int MOD = 1000000007; int n; std::cin >> n; std::vector> edges(n - 1); for (auto& [a, b, c] : edges) { std::cin >> a >> b >> c; --a; --b; } std::vector>> graph(n); for (const auto [a, b, c] : edges) { graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } std::vector depth(n, -1); depth[0] = 0; std::queue queue; queue.push(0); while (!queue.empty()) { const auto top = queue.front(); queue.pop(); for (const auto [next, _] : graph[top]) { if (depth[next] == -1) { depth[next] = depth[top] + 1; queue.push(next); } } } std::vector from_leaf(n); std::iota(from_leaf.begin(), from_leaf.end(), 0); std::sort(from_leaf.rbegin(), from_leaf.rend(), [&depth](const int i, const int j) {return depth[i] < depth[j]; }); long long int result{ 0 }; for (auto d = 0; d < 30; ++d) { long long int one_path{ 0 }; std::vector end_path(n, 0); for (const auto node : from_leaf) { int count{ 0 }; for (const auto [child, c] : graph[node]) { if (depth[child] <= depth[node] || (((c >> d) & 1) == 0)) continue; one_path += (count + 1LL) * (end_path[child] + 1LL); count += end_path[child] + 1; } end_path[node] = count; } result += ((one_path % MOD) << d) % MOD; } result %= MOD; std::cout << result << '\n'; }