C. Colorful Pictures
time limit per test
15 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Examples
Input
4
2 1
2 3
2 4
Output
16
Input
2
1 2
Output
2
Input
23
3 13
9 14
8 20
11 4
8 22
6 10
6 2
18 8
19 8
8 9
4 8
4 5
3 8
23 8
15 12
1 14
16 8
2 12
7 6
8 21
8 6
17 8
Output
616298673