Trade
Time limit1.5sMemory limit1024 MB
Sum all pairwise shortest distances in a complete binary tree with upward edges and m ancestor-to-descendant edges, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Shortest path, Tree, Divide and conquer
- Solved
- No attempts yet
Problem
In recent years, commercial development in country A has been rapid, but domestic road construction has not kept up with it. This clearly restricts people's trade and worries the managers greatly.
Specifically, country A has cities, and city is the capital. For every non-capital city , there is a single one-way road from city to city . These are called "first-class roads", and city is called the "superior city" of city .
In addition, there are directed roads. The -th road goes from city to city . All of them have one special property: starting from city and repeatedly walking along first-class roads toward the superior city, one can always reach city . These roads are called "second-class roads".
Each road has a length. For any two cities and , let be the minimum total length of the roads on a path from to . Some pairs may be unreachable because of serious defects in the road construction. In that case is defined as . A city needs no road to reach itself, so is defined as .
The managers want to measure the convenience of trade through these values. Listing every value takes too long, so they only want the sum . Please help them compute it.
Since the answer may be very large, output it modulo .
Input
The first line contains two positive integers and .
The second line contains positive integers. The -th integer is the length of the first-class road from city to city .
Then lines follow. Each line contains three positive integers , , and , which describe a second-class road from city to city with length .
Output
Output one line containing an integer, the answer modulo .
Constraints
For all test data, , , , and .