Epidemic Flu

Time limit1sMemory limit128 MB

Summary
Find the set of people infected on day K using repeated multiplication modulo M against the day one infected set.
Level

Hard8 of 10

Topics
Number theory, Math, Graph, Brute force
Solved
No attempts yet

Problem

An epidemic flu has started spreading through the village where Sang-geun lives. The village has MM people, numbered 00 to M−1M-1. The flu lasts exactly one day, so a single person can catch it several times (on different days).

The flu was brought in by people who had visited another village and come back. Their numbers are all known, and on the first day exactly these people have the flu.

From the second day on, the flu spreads each day by the following rule: for every person aa who had the flu on the immediately preceding day and every person bb who had it on the first day, the person numbered p=(a×b) mod Mp = (a \times b) \bmod M catches the flu. Here aa and bb may be equal.

For example, suppose the village has 101101 people and the people infected on the first day are 55 and 5050. On the second day the infected people are 2525, 4848 (250 mod 101250 \bmod 101), and 7676 (2500 mod 1012500 \bmod 101). One of the people infected on the third day is 7777 ((48×50) mod 101(48 \times 50) \bmod 101).

Write a program that finds everyone who has the flu on day KK.

Input

The first line contains three integers KK, MM, and NN (1≤K≤10181 \le K \le 10^{18}, 3≤M≤15003 \le M \le 1500, N<MN < M); NN is the number of people infected on the first day.

The second line contains the numbers of the people infected on the first day, separated by spaces.

Output

On one line, print the numbers of the people who have the flu on day KK, in ascending order, separated by spaces.

Examples3

  1. Example 1

    Input
    1 100 3
    1 2 3
    
    Expected output
    1 2 3
    
  2. Example 2

    Input
    2 100 3
    1 2 3
    
    Expected output
    1 2 3 4 6 9
    
  3. Example 3

    Input
    10 101 2
    5 50
    
    Expected output
    36 44 57 65