Lucky Tickets

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

The government plans a transport reform which will change ticket numbers. The new ticket numbers will consist of qq digits in nn-ary number system, where qq is a prime number. Leading zeros are allowed.

A ticket is considered lucky if the product of all its digits added to the sum of all its digits, taken modulo nn, equals ss. Additionally, every lucky ticket has a degree of luckiness: the degree of luckiness of the ticket with digits a_1a_2a_qa\_1 a\_2 \ldots a\_q equals (a_1+1)(a_2+2)(a_q+q)+20a_1+21a_2++2q1a_q.(a\_1 + 1) (a\_2 + 2) \ldots (a\_q + q) + 2^0 a\_1 + 2^1 a\_2 + \ldots + 2^{q-1} a\_q\text{.} 

For reform report, it is needed to calculate the sum of luckiness of all lucky tickets modulo qq.

입력

The first line contains three integers nn, ss and qq (2n1062 \le n \le 10^6, 0s<n0 \le s < n, 2q1062 \le q \le 10^6, qq is prime).

출력

Print the sum of luckiness of all lucky tickets modulo qq.