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: Accepted)

def S1(r,T,M):
  # 1+r+...+r^{T-1}
  # (r^T-1)/(r-1)
  # mod M(r-1)で計算する
  if r == 1:
    return T%M
  x = pow(r,T,M*(r-1)) - 1
  return (x//(r-1))%M

def S2(r,T,M):
  # sum ir^i
  if r == 1:
    x = T*(T-1)//2
    return x%M
  Mr = M*(r-1)
  x = (T-1) * pow(r,T,Mr)
  x -= S1(r,T,Mr)
  x += 1
  x //= (r-1)
  return x

def S(A,B,L,M):
  result = 0
  d = len(str(A)) # 桁数
  start = A
  r = 10**d
  while True:
    n1 = ((r-1)-A)//B+1
    items = min(n1,L) # その桁の項数
    last = A + (items-1)*B
    x = last * S1(r,items,M)
    x -= B * S2(r,items,M)
    result *= pow(r,items,M)
    result += x
    result %= M
    # パラメータの更新
    r *= 10
    d += 1 # 不要かも
    L -= items
    A += B*items
    if L <= 0:
      break
  return result%M

L,A,B,M = map(int,input().split())
answer = S(A,B,L,M)
print(answer)