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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }


using namespace std;

typedef vector<ll> vec;
typedef vector<vec> mat;

ll M;

// A*Bの計算
mat mul(mat &A, mat &B) {
    mat C(A.size(), vec(B[0].size()));
    for (int i = 0; i < A.size(); i++) {
        for (int k = 0; k < B.size(); k++) {
            for (int j = 0; j < B[0].size(); j++) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
            }
        }
    }

    return C;
}

// A^nの計算
mat pow(mat A, ll n) {
    mat B(A.size(), vec(A.size()));
    for (int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while(n > 0) {
        if (n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;


}


int main() {
    ll L, A, B;
    cin >> L >> A >> B >> M;
    ll ub[19];
    ub[0] = 0;
    for (auto i = 1; i <= 18; i++)
    {
        ll x = ub[i - 1];
        if ((to_string(A + B * x)).size() != i)
        {
            ub[i] = ub[i - 1];
            continue;
        }
        ll ok = x;
        ll ng = L;
        while (abs(ok - ng) > 1)
        {
            ll t = (ok + ng) / 2;
            if ((to_string(A + B * t)).size() == i)
            {
                ok = t;
            }
            else
            {
                ng = t;
            }
        }
        ub[i] = ng;
//        cout << ub[i] << endl;
    }
    ll ans = 0;
    vector<ll> d = {0, A, 1};
    REP(i, 1, 19) {
        ll x = 1;
        for (ll j = 0; j < i; j++) {
            x *= 10;
            x %= M;
            if (x < 0) x += M;
        }
        mat mm(3, vec(3));
        mm[0] = {x, 1, 0};
        mm[1] = {0, 1, B};
        mm[2] = {0, 0, 1};
        ll y = ub[i] - ub[i-1];

        if (y == 0) {
            continue;
        }

        mat m2 = pow(mm, y);
        vector<ll> r(3, 0);
        rep(i, 3) {
            rep(j, 3){
                r[i] += (m2[i][j]*d[j]) % M;
                if (r[i] < 0) r[i] += M;
            }
            r[i] %= M;
            if (r[i] < 0) r[i] += M;
        }

        d = r;


    }

    cout << d[0] << endl;



}