Under construction

Still being built. Things may change

The End of the Pilgrimage

Time limit1sMemory limit1024 MB

Summary
Given the last N visited sites, find the expected time until the last N visits are all distinct, printed modulo the prime X.
Level

Hard8 of 10

Topics
Probability, Dynamic programming, Math
Solved
No attempts yet

Problem

A pilgrim keeps making a pilgrimage that seems like it will never end. The pilgrim moves from the holy site where they are to another holy site, and each time they arrive at a holy site, they pray to God. They repeat this over and over.

There are NN holy sites on earth, numbered from 1 to NN. Moving from holy site ii to holy site jj takes exactly Li,jL_{i,j} minutes. The time to travel between two different holy sites can differ by direction. When the pilgrim arrives at holy site ii, they pray there for exactly PiP_i minutes.

After finishing a prayer, the pilgrim moves to the next holy site immediately, following God's revelation. God picks one of the other N−1N-1 holy sites, excluding the one where the pilgrim is, with equal probability 1N−1\frac{1}{N-1}, and tells the pilgrim. The pilgrim goes there at once. God is almighty and the pilgrim is very quick, so the time between a move and a prayer, or between a prayer and a move, is 0 minutes.

If the last NN holy sites the pilgrim visited, counted when a prayer ends, are all different, the pilgrimage ends and the pilgrim enters the kingdom God has prepared.

The last NN holy sites the pilgrim visited are given in order. Find the expected time from the moment the pilgrim finishes praying at their current holy site and starts moving again until the pilgrimage ends.

Input

The first line contains the number of holy sites NN and a prime XX (109≤X≤109+10410^9 \leq X \leq 10^9 + 10^4). The use of XX is explained in the output section.

The second line contains the prayer times P1,P2,⋯ ,PNP_1, P_2, \cdots, P_N (1≤Pi≤1061 \leq P_i \leq 10^6), where PiP_i is the time spent praying at holy site ii.

The next NN lines contain the travel times. The ii-th of these lines contains Li,1,Li,2,⋯ ,Li,NL_{i,1}, L_{i,2}, \cdots, L_{i,N} (0≤Li,j≤1060 \leq L_{i,j} \leq 10^6, Li,i=0L_{i,i}=0).

The next line contains the number of queries QQ (1≤Q≤1051 \leq Q \leq 10^5).

Each of the next QQ lines contains NN positive integers M1,M2,⋯ ,MNM_1, M_2, \cdots, M_N (1≤Mi≤N1 \leq M_i \leq N, Mi−1≠MiM_{i-1} \neq M_i). M1M_1 is the oldest visited holy site, and MNM_N is the most recently visited one.

Output

Print QQ lines of answers. For every valid input, the expected value is proven to be a rational number.

Let the expected value for the ii-th visit sequence be the irreducible fraction PiQi\frac{P_i}{Q_i}. On the ii-th line, print the integer RiR_i with 0≤Ri<X0 \leq R_i < X that satisfies Pi≡QiRi(modX)P_i \equiv Q_i R_i \pmod{X}. It is verified that such an RiR_i exists and is unique for every valid input.

Examples2

  1. Example 1

    Input
    3 1000000007
    1 4 9
    0 1 2
    3 0 4
    5 6 0
    3
    1 2 3
    2 1 2
    3 2 1
    
    Expected output
    0
    666666688
    0
    
  2. Example 2

    Input
    5 1000009999
    29 68 49 31 77
    0 83 12 72 81
    93 0 46 54 22
    19 51 0 97 35
    39 24 90 0 15
    79 32 41 55 0
    5
    1 2 5 3 5
    2 3 4 5 2
    1 2 1 2 1
    5 2 3 4 2
    3 4 2 3 1
    
    Expected output
    847386114
    834075996
    404434664
    276590794
    316334025