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 Python (3.4.3) (judge verdict: Wrong Answer)

#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    #二分法を使って高速に求める
    def f(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return ((f(N - 1)%mod * pow(10 , k,mod)) %mod + 1)%mod
        else:
            x = f(N//2)
            return ((x * pow(10 , k * (N//2) ,mod)) %mod + x)%mod
        
    
    def g(N):
        if N == 0:
            return 0
        elif N % 2 == 1:
            return ((g(N - 1) * pow(10 , k,mod))%mod + ((N-1) * B)%mod) %mod
        else:
            x = g(N // 2)
            return ((x * pow(10 , k * (N//2),mod))%mod + x + ((N//2) * B * f(N//2))%mod  )%mod
            




    L, A, B, mod = MI()
    first = A
    last = A + (L - 1) * B
    ans=0
    for k in range(1, 19):
        if (last < 10 ** (k - 1) or 10 ** k - 1 < first):
            continue
        #k桁の整数のgroupにおける、初項Ag
        Ag = ceil((10 ** (k - 1) - A) / B) * B + A
        #print("初項",Ag)

        #groupの要素数
        if last <= 10**k -1:
            Lastg = last
            N = (Lastg-Ag)//B + 1
        else:
            N = (((10 ** k) - 1 - Ag) // B) + 1
        N%=mod

        #N*k桁ずらす
        ans *= (10 ** (N * k)) % mod
        ans%=mod

        #このグループにおける値の計算
        ans += Ag * f(N)
        ans += g(N)
        ans%=mod
    print(ans)
    
if __name__ == '__main__':
    main()