問題一覧 > 通常問題

No.2356 Back Door Tour in Four Seasons

レベル : / AI推定難易度 : 1600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 74
作問者 : 👑 AngrySadEight / テスター : 👑 hamamu kumakuma
お気に入りにしたユーザー ProblemId : 9405 / 自分の提出
問題文最終更新日: 2023-06-17 00:10:57
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 393 (順位表) の他の問題:

Problem Statement

The Kingdom of yuki has $N$ towns. In the Kingdom of yuki, the season differs from town to town, and the season of the $i$-th town is represented by $S_i$. If $S_i$ is U, the town is in summer; if F, autumn; if W, winter; and if P, spring.

The $i$-th town has $A_i$ one-way doors. Using one door, you can warp to one specific other town in the Kingdom of yuki.

Now, consider visiting a total of 4 towns by first choosing one town to visit, and thereafter repeatedly warping through one of the doors in the current town. Here, you move from the $j$-th visited town $(j =1,2,3)$ to the $j+1$-th visited town with $1$ warp. No means of travel other than warping through doors can be used.

You want the seasons of the 4 visited towns to be, in the order of visiting, "summer → autumn → winter → spring".

There are $(N-1)^{A_{all}}$ combinations of door destinations in total (where $A_{all}$ is the total number of doors). Over all of them, find the sum of the number of ways of visiting towns such that the seasons of the visited towns are "summer → autumn → winter → spring", modulo $998244353$.

Two ways of visiting towns are distinguished if there exists some $j(1 \leq j \leq 4)$ such that the $j$-th visited town differs. Note that ways of visiting towns in which the $j$-th visited town is the same for all $j(1 \leq j \leq 4)$ are not distinguished, even if the doors used for moving differ.

Constraints

  • $2 \leq N \leq 3 \times 10^5$
  • $S_i$ is one of U, F, W, P.
  • $1 \leq A_i \leq 10^5$
  • $N, A_i$ are integers.

Input

The input is given from Standard Input in the following format.

$N$
$S_1$ $A_1$
$S_2$ $A_2$
$\cdots$
$S_N$ $A_N$

Output

Output the answer.

Sample

Sample 1
Input
4
U 1
F 1
W 1
P 1
Output
3

There is one town each of summer, autumn, winter, and spring, and each of them has $1$ door.

Only when the door in the summer town leads to the autumn town, the door in the autumn town leads to the winter town, and the door in the winter town leads to the spring town, is there exactly $1$ way of visiting towns that satisfies the condition.

The door in the spring town may lead to any of the other $3$ towns. For any other combination of destinations, no way of visiting towns satisfies the condition, so summing over them gives the answer $3$.

Sample 2
Input
4
U 3
F 1
W 1
P 1
Output
57

Compared with the previous case, the number of doors in the summer town has increased to $3$.

Note that, once the destination of each door is fixed, ways of visiting towns in which the $4$ visited towns are the same, including their order, are not distinguished, even if the doors used for moving differ.

Sample 3
Input
4
P 2017
U 8
F 11
W 16
Output
317248232

Output the remainder when divided by $998244353$.

Sample 4
Input
16
P 1
U 2
F 3
W 4
P 5
U 6
F 7
W 8
P 9
U 10
F 11
W 12
P 13
U 14
F 15
W 16
Output
958756683

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。