Subsequence Hashes

Print the polynomial hashes of the K lexicographically smallest non-empty subsequences of the given array.

Hard8HeapSortingCombinatoricsNo attempts yetTime limit1sMemory limit256 MB

Problem

You are given an array of NN integers. Sort every non-empty subsequence of that array lexicographically and call the result s1,s2,,sqs_1, s_2, \dots, s_q. A subsequence is an array obtained by deleting zero or more elements from the original array. Several subsequences can be equal to one another, and q=2N1q = 2^N - 1.

Array AA is lexicographically smaller than array BB if Ai<BiA_i < B_i at the first position ii where the two arrays differ, or if AA is a strict prefix of BB.

The hash of an array with values v1,v2,,vpv_1, v_2, \dots, v_p is defined as

h(s)=(v1Bp1+v2Bp2++vp1B+vp)modMh(s) = (v_1 B^{p-1} + v_2 B^{p-2} + \dots + v_{p-1} B + v_p) \bmod M

where BB and MM are given integers. For a given KK, compute h(s1),h(s2),,h(sK)h(s_1), h(s_2), \dots, h(s_K).

Input

The first line contains the integers NN, KK, BB, MM (1N1000001 \le N \le 100\,000, 1K1000001 \le K \le 100\,000, 1B,M10000001 \le B, M \le 1\,000\,000).

The second line contains the integers a1,a2,,aNa_1, a_2, \dots, a_N (1ai1000001 \le a_i \le 100\,000).

Every input satisfies K2N1K \le 2^N - 1.

Output

Print KK lines. Line jj contains h(sj)h(s_j).

Note

In the first example the sorted subsequences are s1=[1]s_1 = [1], s2=[1,2]s_2 = [1, 2], s3=[2]s_3 = [2], so h(s1)=1mod5=1h(s_1) = 1 \bmod 5 = 1, h(s2)=(1+2)mod5=3h(s_2) = (1 + 2) \bmod 5 = 3 and h(s3)=2mod5=2h(s_3) = 2 \bmod 5 = 2.

In the second example they are s1=[1]s_1 = [1], s2=[1]s_2 = [1], s3=[1,1]s_3 = [1, 1], s4=[1,3]s_4 = [1, 3]. Two elements have the value 1, so [1][1] appears twice. The hashes are h(s1)=1mod3=1h(s_1) = 1 \bmod 3 = 1, h(s2)=1mod3=1h(s_2) = 1 \bmod 3 = 1, h(s3)=(1×2+1)mod3=0h(s_3) = (1 \times 2 + 1) \bmod 3 = 0 and h(s4)=(1×2+3)mod3=2h(s_4) = (1 \times 2 + 3) \bmod 3 = 2.