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++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;

vvl dot(vvl a, vvl b, ll M){
  vvl c = vv(a.size(), b[0].size(), 0, ll);
  for(int i=0;i<a.size();i++){
    for(int j=0;j<b[0].size();j++){
      for(int k=0;k<b.size();k++){
        c[i][j] = (c[i][j] + (a[i][k]*b[k][j])%M)%M;
      }
    }
  }
  return c;
}
int main(int argc, char const *argv[]) {
  ll L, A, B, M;
  std::cin >> L >> A >> B >> M;
  std::vector<ll> keta(19, 0);
  ll now = 1, wa=0;
  for(int i=0;i<18;i++){
    now *= 10;
    if(A >= now) keta[i+1]=0;
    else{
      keta[i+1] = (now-1 - A)/B + 1;
      keta[i+1]-=wa;
    }
    if(wa + keta[i+1] <= L) wa += keta[i+1];
    else keta[i+1] = L - wa, wa = L;
    //std::cout << keta[i+1] << " " << now-1 << " " << A << '\n';
  }
  now = 1;
  vvl ans = vv(1, 3, 0, ll);
  ans[0][0] = 0, ans[0][1] = A, ans[0][2] = 1;
  for(int i=1;i<19;i++) {
    now *= 10;
    vvl c = vv(3, 3, 0, ll);
    c[0][0] = now, c[1][0] = 1, c[1][1] = 1, c[2][1] = B, c[2][2]=1;
    while(keta[i]>0){
      if(keta[i]%2==1) ans = dot(ans, c, M);
      c = dot(c, c, M);
      keta[i]/=2;
    }
  }
  std::cout << ans[0][0] << '\n';
  return 0;
}