In recent years, the commercial development in country A has been rapid, but the domestic road construction has not kept up with the pace, which has clearly become a restriction on people's trade and has caused great concern for the managers.
Specifically, there are cities in country A, where city is the capital. For all non-capital cities , there is a single one-way road that starts from city and reaches city . For convenience, these roads are called "first-class roads", and the city is called the "superior city" of city .
In addition, there are directed roads, where the -th road goes from city to city , and they all have a special property: starting from city and continuously walking along the first-class roads towards the "superior city", one can always reach city . These roads are called "second-class roads".
Each road has a corresponding length. Therefore, for any two cities and in country A, one can calculate the minimum value of the sum of the lengths of the roads passed through from city to city , and this value is denoted as . However, due to serious defects in the road construction in country A, it may be impossible to reach city from city . For convenience, is defined as in this case. At the same time, a city does not need to pass through any road to reach itself, so is defined as.
Now, the managers hope to calculate these values in order to measure the convenience of people's trade. However, since there are too many cities in country A, listing all these values one by one is too time-consuming. Therefore, the managers only hope to find the sum of all values, that is, , and they hope that you can help.
Since the answer may be very large, you only need to output the answer modulo .
Input Specification
The first line of input contains two positive integers and .
The second line of input contains positive integers. The -th integer represents the length of the "first-class road" from city to city .
Then lines follow, each containing three positive integers and , representing a "second-class road" from city to city with length .
Output Specification
Output a line containing an integer, representing the answer, taken modulo .
Sample Input 1
2 1
2 1
1 2 2
Sample Output 1
8
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
ex_trade2.in
andex_trade2.ans
). - Sample 3 (
ex_trade3.in
andex_trade3.ans
). - Sample 4 (
ex_trade4.in
andex_trade4.ans
).
Constraints
For all test data, it is guaranteed that: .
Test ID | Additional Constraints | ||
---|---|---|---|
None | |||
A | |||
None | |||
A | |||
None |
Additional Constraint A: It is guaranteed that every "second-class road" starts from the capital (city ).
Comments