#P8208. [THUPC 2022 初赛] 骰子旅行
[THUPC 2022 初赛] 骰子旅行
Problem Description
Before band f goes on tour, by tradition they first organize a dice trip for the band members to relax. A dice trip includes locations, numbered . The band members agree in advance to meet at ; on the day of the dice trip, everyone arrives at the meeting point , and the dice trip officially begins.
A major fun part of a dice trip is that a die decides the next destination. Of course, the die does not have to be six-sided. We can assume that if the band members are currently at location , then the next destination is chosen uniformly at random from distinct candidate locations, namely . Let the result of the -th roll be . Then before the -th roll, they will go to to roll the die. The first roll is performed at the starting point . Since the band still needs to rehearse for the tour afterward, they agree in advance that no matter which locations they go to, after rolling the die for the -th time and then going to , the dice trip must end.
Of course, enjoying the scenery of is also a big part of the trip. Whether they have been there before or not, every time they arrive at a location , the band members will fully enjoy the views and taste the local food. However, if they have been to before, then before rolling the -th die, the keyboardist S, who is responsible for rolling, will definitely say: “The last time we came to seems like it was at time . Last time we rolled here. I wonder what we will roll this time.” The drummer Y especially likes this kind of chatter, so every time S says this sentence, he writes down . In particular, if is a location visited before, then S will say: “The last time we came to seems like it was at time . Last time we rolled here, but this time we will not roll, because the dice trip is about to end here.” Of course, Y will also write down this .
As a summary of this dice trip, Y will add up all the recorded , and use it as S’s “chatter index”.
Band f’s next tour is about to start, so S is planning to take everyone on a dice trip again. Hearing that you are a fan of f, S comes to you and hopes you can help him compute the expected value of the chatter index for this dice trip.
Input Format
The first line contains three positive integers , representing the number of locations that may be involved, the starting location of the dice trip, and the number of die rolls during the dice trip.
The next lines describe the transitions. In the -th line , first input a positive integer , representing the number of candidate next destinations from location ; then input positive integers, representing these locations . It is guaranteed that for any , the input locations are pairwise distinct.
Output Format
Output one number representing the expected value of the chatter index. Suppose the expected value of the chatter index is reduced to the simplest fraction (i.e. and are coprime). Output such that and . It can be proven that under the Constraints of this problem, such an exists and is unique.
5 1 2
3 2 3 4
2 1 5
2 1 5
2 1 5
3 2 3 4
499122178
7 1 4
6 2 3 4 5 6 7
6 1 3 4 5 6 7
6 1 2 4 5 6 7
6 1 2 3 5 6 7
6 1 2 3 4 6 7
6 1 2 3 4 5 7
6 1 2 3 4 5 6
274979351
Hint
【Sample Explanation 1】
The paths that contribute to the answer are: start from node , go to any node among , and return to node . For a certain node , the probability of going to node and then returning to node is , and the contribution is , so the expectation is
Since $499122178 \times 2 = 998244356 \equiv 3 \pmod {998244353}$, we know that modulo is , so the correct output is .
【Sample Explanation 2】
The answer before taking modulo is , and $432\times 274979351 = 118791079632 \equiv 1625 \pmod{998244353}$, so the answer modulo is .
【Sample 3】
See the attachment.
【Constraints】
For of the testdata, it is guaranteed that , , , , , , and $\forall 1\le i\le N, \forall 1\le j_1<j_2\le m_i, l_{i, j_1}\ne l_{i, j_2}$。
Translated by ChatGPT 5