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 C++ (GCC 9.2.1) (judge verdict: Accepted)

template<typename T, typename F> const T bsearch(T l, T h, const T& err, const F& f) {
  if(f(l)) return l;
  while(h - l > err) {
    auto m = (l + h) / 2;
    f(m) ? (h = m) : (l = m);
  }
  return h;
}

#include<bits/stdc++.h>
using namespace std;

using ULL = unsigned long long;

ULL L, A, B, M;

ULL power(ULL n, ULL m) {
  if(!m) return 1;
  if(m&1) return n * power(n, m-1) % M;
  return power(n*n % M, m/2);
}

ULL f(ULL n, ULL D) {
  if(!n) return 0;
  if(n&1) return (1 + D*f(n-1,D)) % M;
  auto m = n/2;
  auto x = f(m,D);
  return (x + power(D,m)*x) % M;
}

ULL g(ULL n, ULL D, ULL B) {
  if(!n) return 0;
  if(n&1) return (D*g(n-1,D,B) + B*(n-1)) % M;
  auto m = n/2;
  auto x = g(m,D,B);
  return (power(D,m)*x + x + (B*m % M)*f(m,D)) % M;
}

int main() {
  cin >> L >> A >> B >> M;
  ULL ans = 0;
  for(ULL D=1; D<=1e18; D*=10) {
    auto l = bsearch<ULL>(0, L, 1, [&](auto x) {return D <= A + B*x;});
    auto h = bsearch<ULL>(0, L, 1, [&](auto x) {return 10*D <= A + B*x;});
    if(L <= l) break;
    if(A + B*l < D) break;
    if(A + B*h < 10*D) ++h;
    if(L < h) h = L;
    if(!(l < h)) continue;
//     cerr<<"["<<l<<", "<<h<<")"<<endl;
    auto n = h - l;
    auto b = B % M;
    auto a = (A + b * l % M) % M;
    auto d = 10*D % M;
    (ans *= power(d, n)) %= M;
    (ans += a * f(n, d) % M) %= M;
    (ans += g(n, d, b) % M) %= M;
//     cerr<<"a="<<a<<"; n="<<n<<"; D="<<D<<"; f="<<f(n,d)<<"; g="<<g(n,d,b)<<"; ans="<<ans<<endl;
  }
  cout << ans << endl;
}