Under construction

Still being built. Things may change

Destroying the spire

Time limit1sMemory limit512 MB

Summary
Given N, M, and a seed, compute the monster strength on floor N, a power tower of values g(k), modulo M.
Level

Medium6 of 10

Topics
Math, Number theory, Recursion
Solved
No attempts yet

Problem

Hyeongi often plays a game called Destroying the Spire. The player enters the spire at floor 1 and climbs one floor at a time to the top, defeating every monster on each floor. After clearing the spire, a new attempt brings different monsters.

When a new game starts, a seed (Seed) is generated at random. The seed determines the strength (Strength) of the monsters on each floor. The seed is a 10-character string made of letters A to Z, a to z, and digits 1 to 9 (0 is not used). To compute strength, each character of the seed is replaced with the integer value in the table below.

CharacterValue
1, 2, ..., 91, 2, ..., 9
A, B, ..., Y, Z10 to 35
a, b, ..., y, z36 to 61

For example, when the seed is AB1z8DcdT4, the first character 'A' has the value 10. Seed[i]Seed[i] is the character at position i+1i+1 counting from the left.

The monster strength on floor 1 is 1. For floor k≠1k \neq 1, the strength is computed as follows.

f(i,k)=Seed[(i×k) mod 10]f(i,k) = Seed[(i \times k) \bmod 10]

g(k)=∑i=110f(i,k)×kg(k) = \sum_{i=1}^{10} f(i,k) \times k

Strength(k)=g(k)Strength(k−1)Strength(k) = g(k)^{Strength(k-1)}

Find the strength of the monster on the top floor, floor NN, of a spire game with NN floors, modulo MM.

Input

The first line contains two integers NN and MM. (1≤N,M≤1091 \le N, M \le 10^9)

The second line contains SeedSeed. SeedSeed is a 10-character string made of letters A to Z, a to z, and digits 1 to 9.

Output

Print the strength of the monster on floor NN modulo MM on the first line.

Examples2

  1. Example 1

    Input
    4 37
    AB1z8DcdT4
    
    Expected output
    1
    
  2. Example 2

    Input
    8 22
    65n1Ad87oQ
    
    Expected output
    10