#P5888. 传球游戏

传球游戏

Background

In Yangcheng, there are people who are good at playing cuju. During the Football Association Cup, at the northeast corner of the campus, two fields are set up. The cuju players stand in the field: nn people, one ball, two goals, three referees, that is all. The audience sits around. After a while, as soon as the whistle on the field blows, everyone becomes silent, and no one dares to make noise.

At that moment, the sound of passing, the faint sound of wind, the sound of players sprinting, the coach shouting, and the cheerleaders cheering all burst out together, and every wonderful sound is present. The whole audience stretches their necks, looks sideways, smiles, and sighs silently, thinking it is amazing.

Soon, one of our players makes a long pass. Their player intercepts it and charges toward our goal.

Then we see the goalkeeper oql standing at the goal, as if thinking about something.

Problem Description

It turns out he is thinking about this problem:

There are nn players standing in a circle, numbered from 11 to nn. At the beginning, the ball is in the hands of player 11. There are mm passes in total. In each pass, the ball must be passed to someone, but it cannot be passed to oneself. Find the number of ways such that after the mm-th pass, the ball is passed back to player 11.

But he thinks this problem is too easy, so he adds kk restrictions. Each restriction is of the form a,ba,b, meaning that player aa cannot pass the ball to player bb.

To bring oql’s attention back to the match as soon as possible, you need to tell him this number of ways in the shortest time.

You only need to output the result modulo 998244353998244353.

Input Format

The input consists of k+1k+1 lines:

The first line contains three integers n,m,kn,m,k, which represent the number of players, the number of passes, and the number of restrictions.

The next kk lines each contain two integers ai,bia_i,b_i, indicating that player aia_i cannot pass the ball to player bib_i.

The data guarantees that there do not exist different i,ji,j such that ai=aja_i=a_j and bi=bjb_i=b_j.

Output Format

Output one integer, representing the number of valid ways for the ball to return to player 11 after mm passes, modulo 998244353998244353.

2 1 0
0
3 3 0
2
7 13 5
1 3
4 5
5 4
6 1
2 2
443723615

Hint

For 10%10\% of the data, k=0k=0.

For another 15%15\% of the data, n500n\leq 500.

For another 20%20\% of the data, n5×104n\leq 5\times 10^4.

For another 20%20\% of the data, k300k\leq 300.

For 100%100\% of the data, 1n1091\leq n\leq 10^9, 0m2000\leq m\leq 200, 0kmin(n×(n1),5×104)0\leq k \leq \min(n\times(n-1),5\times 10^4), 1ai,bin1\leq a_i,b_i\leq n, it is not guaranteed that aia_i and bib_i are different.

Translated by ChatGPT 5