manymsts

Many MSTs

There is a weighted undirected graph of n nodes which are labeled from 0 to n1. The graph initially has y edges where the ith edge has a weight ci and connects nodes ai and bi.

x operations are then performed. In the ith operation, for all pairs of nodes where their label differs by di a edge of weight wi is created connecting the two nodes.

After these opertains, let the final state of the graph be G. Let the connected components in G be G0,G1,...,Gk1 and let f(Gi) be the sum of weights of the minimum spanning tree of Gi.

Find i=0k1f(Gi) modulo 998244353.

Limits

1n1018
1x,y5104
1di<n
0ai,bi<n
1ci,wi998244353

Subtasks # Score Constraits
1 4 n2105,x10
2 8 n2105
3 6 x=2,y=0
4 18 x=2,y5104
5 12 x1000,y=0
All ci,wi=1
6 12 x1000,y200
7 12 y=0
8 10 All ci,wi=1
9 18 -

Input format

The first line contains three non-negative integers n,x,y.

The next x lines contains 2 integers, where the ith of these lines represents di and wi

The next y lines contains 3 integers, where the ith of these lines represents ai, bi and ci

Output format

Output one non-negative integer, the answer modulo 998244353.

Samples

Sample Input 1 Sample Output 1
13 2 3
4 16
5 17
10 2 3
0 7 19
5 6 12
177
Sample Input 2 Sample Output 2
80 5 10
35 5
68 7
4 11
67 15
21 18
1 20 13
33 48 5
37 68 16
64 72 4
22 11 13
73 17 1
24 71 9
71 30 9
16 18 2
13 2 4
512


Submitting .cpp to 'manymsts'


You're not logged in! Click here to login

Time Limit: 4 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: LOJ #3635

Subtask Score
1 4
2 8
3 6
4 18
5 12
6 12
7 12
8 10
9 18
10 0