#P6154. 游走

游走

Background

zbw is wandering in City B.

Problem Description

City B can be seen as a directed acyclic graph with nn vertices and mm edges. Multiple edges may exist.

zbw walks randomly in City B. He will randomly choose one path among all paths, and every path is chosen with equal probability. The starting vertex and the ending vertex of a path may be the same.

Define the length of a path as the number of edges it passes through. You need to compute the expected length of the path zbw walks, and output the answer modulo 998244353998244353.

Input Format

The first line contains two integers n,mn, m.

The next mm lines each contain two integers x,yx, y, indicating that there is a directed edge from xx to yy.

Output Format

Output one integer per line, representing the answer modulo 998244353998244353.

3 2
1 2
3 2
199648871
6 5
1 3
2 3
3 4
4 5
4 6
630470119
5 6
1 2
1 3
4 5
3 4
3 5
2 4
887328315

Hint

Sample explanation: the answers for the samples are 25\dfrac{2}{5}, 2519\dfrac{25}{19}, and 119\dfrac{11}{9}.

Constraints

Test Point ID nn mm Special Property Score per Test Point
1,21,2 10\le 10 None 22
3,4,53,4,5 15\le 15 100\le 100
6,7,86,7,8 100\le 100 103\le 10^3
9,109,10 103\le 10^3 104\le 10^4
11,1211,12 104\le 10^4 105\le 10^5 55
13,1413,14 105\le 10^5 2×105\le 2\times 10^5
15,1615,16 7×105\le 7\times 10^5 1010
1717 10\le 10 =n1= n - 1 Directed Tree
1818 103\le 10^3
1919 104\le 10^4
2020 105\le 10^5

Here, a “Directed Tree” means: if you view the graph as an undirected graph, it is a tree (as in samples 1,21,2).

It is guaranteed that all testdata is generated randomly in some way. This means you may assume that during the algorithm, you can safely perform division modulo 998244353998244353 without worrying about dividing by zero.

Translated by ChatGPT 5