Under construction

Still being built. Things may change

Hash Hacking

Time limit1sMemory limit1024 MB

Summary
Count the length-N sequences over M symbols whose weighted sum mod M equals H, printed modulo 1000000007.
Level

Easy3 of 10

Topics
Math, Number theory
Solved
No attempts yet

Problem

Yeondu, the operator of Green.com, has stopped storing passwords as plain text. From now on, passwords will be stored after applying a hash function. The only hash function Yeondu knows is the rolling hash function often used in competitive programming problems, so Yeondu decided to adapt it.

A Green.com password must have exactly NN characters, and each character must be one of MM allowed characters. If you map each allowed character to an integer starting from 00, a password can be written as an array P=[P0,P1,…,PN−1]P = [P_0, P_1, \dots, P_{N-1}] of length NN where every element is between 00 and M−1M-1 inclusive.

After the password is written as PP, the following hash function hh is applied using a predetermined integer AA.

h(P)=(P0⋅A0+P1⋅A1+⋯+PN−1⋅AN−1) mod Mh(P) = (P_0 \cdot A^0 + P_1 \cdot A^1 + \dots + P_{N-1} \cdot A^{N-1}) \bmod M

For example, take P=[10,30,20]P = [10, 30, 20], A=7A = 7, and M=55M = 55. Then h(P)=(10⋅70+30⋅71+20⋅72) mod 55=(10+210+980) mod 55=45h(P) = (10 \cdot 7^0 + 30 \cdot 7^1 + 20 \cdot 7^2) \bmod 55 = (10 + 210 + 980) \bmod 55 = 45. Here  mod \bmod is the remainder operation, and since 1200=21⋅55+451200 = 21 \cdot 55 + 45, we have 1200 mod 55=451200 \bmod 55 = 45. The hash value is always an integer between 00 and M−1M-1 inclusive.

Jaehyun hacked the hash of an administrator account at Green.com and wants to work backward to find the original password. Green.com allows MNM^N passwords in total. Jaehyun wants to know how many of them have the hash value he recovered. Compute this count for him.

Input

The first line contains three integers NN, MM, and AA (1≤N,M,A≤5 000 0001 \le N, M, A \le 5\,000\,000).

The second line contains the recovered hash value HH (0≤H<M0 \le H < M).

Output

Print the number of passwords whose hash value is HH, modulo 1 000 000 0071\,000\,000\,007 (=109+7= 10^9 + 7).

Examples2

  1. Example 1

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

    Input
    5000000 5000000 5000000
    1
    
    Expected output
    73352076