PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000095. Tổng khoảng cách XOR của mọi cặp đỉnh (Xor Distances)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng khoảng cách XOR của mọi cặp đỉnh (Xor Distances)

Xor Distances

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected weighted tree with nn vertices. Each edge has a nonnegative integer weight ww.

For vertices u,vu,v, let dist⁡(u,v)\operatorname{dist}(u,v) be the bitwise XOR of all edge weights along their unique path, not their arithmetic sum.

Compute the sum of dist⁡(u,v)\operatorname{dist}(u,v) over every unordered pair 1≤u<v≤n1\le u<v\le n, counting each pair exactly once. Print the result modulo 109+710^9+7.

Input

The first line contains nn. The next n−1n-1 lines contain edges u,v,wu,v,w.

Output

Print one integer: the sum of XOR distances over all unordered pairs, modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; 1≤u<v≤n1\le u<v\le n for every edge; 0≤w<2600\le w<2^{60}; edges form a tree.

  • Subtask 2 (30 points): 2≤n≤12002\le n\le 1200; 1≤u<v≤n1\le u<v\le n for every edge; 0≤w<2600\le w<2^{60}; edges form a tree.

  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤u<v≤n1\le u<v\le n for every edge; 0≤w<2600\le w<2^{60}; edges form a tree.

Examples

Input

3
1 2 1
1 3 3

Output

6

Explanation

There are three pairs: dist⁡(1,2)=1\operatorname{dist}(1,2)=1, dist⁡(1,3)=3\operatorname{dist}(1,3)=3, and dist⁡(2,3)=1⊕3=2\operatorname{dist}(2,3)=1\oplus3=2. Their sum is 1+3+2=61+3+2=6.