#P6624. [省选联考 2020 A 卷] 作业题

[省选联考 2020 A 卷] 作业题

Description

Little W has just learned about spanning trees in a discrete mathematics class: a spanning tree TT of an undirected graph G=(V,E)G=(V,E) is a subset of the edge set EE with size V1|V|-1, and the subgraph generated by TT is connected in GG.

While doing today’s homework, Little W got stuck on the following problem:

Given an undirected graph GG with nn vertices and mm edges (both vertices and edges are numbered starting from 11), it is guaranteed that there are no multiple edges and no self-loops in the graph. Each edge has a positive integer weight wiw_i. For a spanning tree TT of GG, define the value of TT as: the greatest common divisor of the weights of the edges in TT multiplied by the sum of these weights, that is:

$$val(T)=\left(\sum\limits_{i=1}^{n-1} w_{e_i}\right) \times \gcd(w_{e_1},w_{e_2},\dots,w_{e_{n-1}})$$

where e1,e2,,en1e_1,e_2,\dots,e_{n-1} are the indices of the edges included in TT.

Little W needs to find the sum of the values of all spanning trees TT of GG. Since the answer may be very large, you only need to output it modulo 998244353998244353.

Input Format

The first line contains two positive integers n,mn,m, representing the number of vertices and edges of GG.

The next mm lines each contain three positive integers ui,vi,wiu_i,v_i,w_i. The ii-th line describes an undirected edge connecting vertex uiu_i and vertex viv_i, with weight wiw_i.

Output Format

Output a single integer in one line, representing the answer modulo 998244353998244353.

3 3
1 2 4
2 3 6
1 3 12
192

Hint

[Sample Explanation 1]

There are three spanning trees in GG:

T1={(1,2),(2,3)}T_1=\{(1,2),(2,3)\}, with value 10×2=2010\times 2=20.

T2={(1,2),(1,3)}T_2=\{(1,2),(1,3)\}, with value 16×4=6416\times 4=64.

T3={(1,3),(2,3)}T_3=\{(1,3),(2,3)\}, with value 18×6=10818\times 6=108.

The total sum is 192192.

[Constraints]

10%10\% of the testdata satisfies: m15m\leq 15.

Another 20%20\% of the testdata satisfies: mnm \leq n.

Another 20%20\% of the testdata satisfies: all wiw_i are equal.

Another 20%20\% of the testdata satisfies: all wiw_i are prime numbers.

100%100\% of the testdata satisfies: $1\leq n\leq 30, 1\leq m \leq \frac {n(n-1)}{2}, 1\leq w_i \leq 152501$。

Translated by ChatGPT 5