Problem:
Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908

Solution in PyPy3 (2.4.0) (judge verdict: Wrong Answer)

from math import ceil,floor
import sys
#sys.setrecursionlimit(10**7)
L,A,B,M=map(int,input().split())

def digit(d):
    if 10**d<=A:
        return A
    return ceil((10**d-A)/B)*B + A

def dot(X,Y):
    L=[[0]*3 for i in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                L[i][j]+=X[i][k]*Y[k][j]
                L[i][j]%=M
    return L
def dot2(X,y):
    L=[0]*3
    for i in range(3):
        for j in range(3):
            L[i]+=X[i][j]*y[j]
            L[i]%=M
    return L

def f(x,t):
    X=[[1,x,1],[0,x,1],[0,0,1]]
    a=t-1
    T=[[1,0,0],[0,1,0],[0,0,1]]
    while a>0:
        if a%2==1:
            T=dot(T,X)
        X=dot(X,X)
        a=a//2
    #print(T,dot2(T,[1,1,1]))
    return dot2(T,[1,1,1])

dig=[0]*19
t=L
for d in range(19):
    s=( digit(d+1)-digit(d) )//B
    if s>=t:
        dig[d]=t
        break
    t-=s
    dig[d] = s
dig=[0]+dig
#print(dig)
fd=0
ans=0
for d in range(19,0,-1):
    if dig[d]==0:
        continue
    tn,sn,c=f(10**d,dig[d])
    a=digit(d-1)
    t=(a*sn+B*(tn-sn))%M
    kai=t*pow(10,fd,M)
    ans+=kai
    ans%=M
    fd+=d*dig[d]
    #print(tn,sn,c,a,fd,kai,digit(d-1))
print(ans)