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: Runtime Error)

//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
//typedef long long ll;
#include <boost/multiprecision/cpp_int.hpp>
typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
//#define i_7 (ll)(1E9+7)
ll i_7=1;
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
////////////////////////////

ll po(ll i,ll p){
    if(p==0)return 1;
    else{
        i=mod(i);
        if(p==1)return i;
        if(p%2==0)return po(mod(i*i),p/2);
        return mod(i*po(i,p-1));
    }
}
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat a,mat b){/*行列aと行列bの積*/
    mat c(a.size(),vec(b[0].size()));
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b[0].size();j++){
            for(int k=0;k<a[0].size();k++){
                c[i][j]=mod(c[i][j]+a[i][k]*b[k][j]);
            }
        }
    }
    return c;
}
ll pow(ll a,ll p){
    ll res=1;
    rep(i,1,p)res=res*a;
    return res;
}

mat pow(mat a,ll n){/*行列aの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,d;cin>>l>>a>>d>>i_7;
    ll S=1;
    ll num[20];memset(num,0,sizeof(num));
    num[19]=l;
    for(int t=1;t<=18;t++){
        if(pow((ll)10,t)<=a)continue;
            ll ok=l,ng=-1;
            while(abs(ok-ng)>1){
                ll mid=(ok+ng)/2;
                if(a+(mid)*d>=pow((ll)10,t-1))ok=mid;
                else ng=mid;
            }
            num[t]=ok;
        
    }
    /*
    rep(i,0,19){
        cout<<i<<" "<<num[i]<<endl;
    }*/
    ll ans=0;
    for(int t=18;t>=1;t--){
        if(num[t]==num[t+1])continue;
        mat B(3,vec(1));
        B={{a+(num[t])*d},{a+(num[t])*d},{1}};
        mat A(3,vec(3));
        A={ {po(10,t),1,d},
            {0,1,d},
            {0,0,1}};
        ll n=num[t+1]-num[t];
        A=pow(A,n-1);
        B=mul(A,B);
        Add(ans,mod(B[0][0]*S));
        S=mod(S*(ll)po(10,n*t));
        //cout<<ans<<" ";
    }
    cout<<mod(ans)<<endl;
    
    
    return 0;
}