문제
Bessie and Farmer John enjoy goat kart racing. The idea is very similar to Go-Kart racing that others enjoy, except the karts are pulled by goats and the track is made from nearby farmland. The farmland consists of
Bessie wants to make a course from nearby farms. A farm is a subset of two or more meadows within which every meadow can reach every other meadow along a unique sequence of roads.
The nearby farmland may contain multiple farms. Suppose there are
To make the course interesting for racers, the total length of the track should be at least
Problem credits: Matt Fontaine
입력
The first line of input contains
Each of the
In at least 70% of the test cases, it is also guaranteed that
출력
Output a single integer, giving the sum of track lengths over all interesting tracks. As the sum of track lengths can be quite large, print the sum of lengths modulo
예제1
5 3 1 12
1 2 3
2 3 4
4 5 6
54
This example has 6 possible tracks
1 --> 2 --> 4 --> 5 --> 1 (length 11)
1 --> 2 --> 5 --> 4 --> 1 (length 11)
2 --> 3 --> 4 --> 5 --> 2 (length 12)
2 --> 3 --> 5 --> 4 --> 2 (length 12)
1 --> 2 --> 3 --> 4 --> 5 --> 1 (length 15)
1 --> 2 --> 3 --> 5 --> 4 --> 1 (length 15)
The answer is
Note that for this problem, the standard time limit is increased to 3 seconds per test case (6 seconds per case for Java and Python).