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<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

Int a, b, m;

Int mod_pow(Int x, Int a, Int m = MOD){
    if(a == 0)return 1;
    Int res = mod_pow(x, a / 2, m);
    res = res * res % m;
    if(a % 2)res *= x;
    return res % m;
}

Int inv(Int x, Int m = MOD){
    return mod_pow(x, m-2, m);
}


//1 + x + x^2+ .. + x^k
Int sum_of_xk(Int x, Int k){
    if(k == 0)return 1;
    if(k % 2 == 0)return (mod_pow(x, k, m) + sum_of_xk(x, k-1)) % m;
    else {
        Int tmp = sum_of_xk(x, (k-1) / 2);
        return tmp * (1 + mod_pow(x, (k+1) / 2, m)) % m;
    }
}

Int sum_of_kxk(Int x, Int k ){
    if(k == 0)return 0;
    if(k % 2 == 0) return (sum_of_kxk(x, k-1) + k * mod_pow(x, k,m) % m) %m;
    Int even = 0, odd = 0;
    even = 2*sum_of_kxk(x*x%m, (k-1) / 2) % m;
    odd = 2 * x % m * sum_of_kxk(x*x%m,(k-1)/2) % m;
    odd += x * sum_of_xk(x*x%m, (k-1)/2) % m;
    odd %= m;
    return (even + odd) % m;
}


Int calc(Int l, Int r, Int k, Int R){
    Int res = 0;
    res += ((a + b * r ) % m) * sum_of_xk(mod_pow(10, k, m), r-l) % m;
    res %= m;
    res -= (b % m) * sum_of_kxk(mod_pow(10, k, m), r-l) % m;
    res %= m;
    if(res < 0)res += m;
    return res * mod_pow(10, R, m) % m;
}

Int keta(Int x){
    if(x == 0)return 0;
    return keta(x / 10) + 1;
}

int main(){
    Int L;
    cin >> L >> a >> b >> m;
    Int least = keta(a);
    Int most = keta(a + b * (L-1));
    Int l = 0;
    Int r = L-1;
    Int res = 0;
    Int R = 0;
    Int tmp = 0;
    Int ketasum = 0;
    for(int k = most;k >= least;k--){
        Int bottom = 0, top = r;
        k = keta(a + b * r);
        while(top - bottom > 1){
            Int mid = (top + bottom) / 2;
            if(keta(a + b * mid) != k)bottom = mid;
            else top = mid;
        }
        l = top;
        if(k == least)l = 0;
        res += calc(l, r, k, R);
        res %= m;
        R += (r - l + 1) * k;
        r = l - 1;
    }
    cout << res << endl;
    return 0;
}