#P15862. [LBA-OI R3 B] 回去!

    ID: 15470 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP数学洛谷原创O2优化洛谷月赛

[LBA-OI R3 B] 回去!

Description

We have a directed graph GG with nn nodes and nn edges, where each node has out-degree exactly 11. Each node is assigned a lowercase letter as its weight.

DZ1000 is about to walk on GG. He uses a string ss and a stack tt to record his journey. Initially, the stack tt is empty.

In each step, he has two choices of action:

  1. Move forward: With probability pp, he moves along the outgoing edge of the current node and pushes the reached node into stack tt.
  2. Go back: With probability (1p)(1-p), he pops the top node from stack tt and returns to the node that is now the top of stack tt. (If stack tt is empty before this action, he cannot do this and must move forward; if the size of stack tt is 11 before this action, he returns to the starting node.)

Immediately after each step, DZ1000 appends the weight of the current node to the string ss.

DZ1000 randomly selects a starting node on GG with equal probability, then takes exactly 2k2k steps. You need to find the probability that the first kk characters of ss are equal to the last kk characters after 2k2k steps. Multiply the answer by nn and take it modulo 998244353998244353.

Note: The weight of the starting node is NOT the first character of string ss.

Input Format

The input contains exactly 33 lines.

The first line has three positive integers n,k,pn, k, p, representing the number of nodes in GG, the number of steps taken, and the probability of moving forward (originally a rational number between 00 and 11, given modulo 998244353998244353).

The second line is a string of lowercase letters with length nn. The ii-th letter cic_i is the weight of node ii.

The third line has nn positive integers vv. The ii-th integer viv_i is the node that the outgoing edge of node ii points to.

Output Format

Output a single integer: the result of multiplying the required probability by nn, modulo 998244353998244353.

3 1 499122177
aba
2 3 1
1
10 10 882315098
nqqmlmnnon
6 3 4 1 4 4 9 4 10 7 
106458929

Hint

Explanation of Sample 1

12\frac{1}{2} modulo 998244353998244353 is 499122177499122177, so the original p=12p = \frac{1}{2}.

The paths are as follows:

  • 1231\to 2\to 3, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string ba — invalid
  • 1211\to 2\to 1, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string ba — invalid
  • 2312\to 3\to 1, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string aa — valid
  • 2322\to 3\to 2, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string ab — invalid
  • 3123\to 1\to 2, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string ab — invalid
  • 3133\to 1\to 3, probability 1n×p=16\frac{1}{n}\times p=\frac{1}{6}, string aa — valid

Total valid probability: 16+16=13\frac{1}{6}+\frac{1}{6}=\frac{1}{3}. Multiply by nn to get 11, so output 11.

Data Constraints

For 100%100\% data: 1n30, 1k301 \le n \le 30,\ 1 \le k \le 30.

Subtask ID nn kk Special Property Score
11 9\le 9 None 55
22 Unrestricted ^ ^ 1010
33 ^ 17\le 17 3030
44 Unrestricted A 55
55 ^ B
66 C 1010
77 None 3535

Special Property A: All cic_i are identical.

Special Property B: For all 1in1\le i\le n, vi=iv_i=i.

Special Property C: For all 1i<n1\le i<n, vi=i+1v_i=i+1, and vn=1v_n=1.