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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::input;
use std::cmp::{min,max};

const SZ:usize = 18;

fn powm(x:i64,k:i64,m:i64) -> i64{
    let mut k = k;
    let mut x = x%m;
    let mut ans = 1;
    while k>0 {
        if k&1==1 {
            ans = ans*x%m;
        }
        x = x*x%m;
        k>>=1;
    }
    return ans;
}

fn f(x:i64,n:i64,m:i64) -> i64{
    // eprintln!("f{}",n);
    if n<=0 { 0 } 
    else if n<=1 { 1 }
    else { 
        let hn = n/2;
        let tmp = f(x,hn,m);
        let lhs = tmp*(1+powm(x,hn,m))%m;
        let rhs = if n%2==1 {powm(x,n-1,m)} else {0};
        (lhs+rhs)%m
    }
}

fn g(x:i64,n:i64,m:i64) -> i64{
    // eprintln!("g{}",n);
    if n<=1 { 0 } 
    else { 
        let hn = n/2;
        let tmp = g(x,hn,m);
        let lhs = (tmp*(1+powm(x,hn,m))%m+hn*f(x,hn,m)%m*powm(x,hn,m)%m)%m;
        let rhs = if n%2==1 {(n-1)*powm(x,n-1,m)} else {0};
        (lhs+rhs)%m
    }
}

fn main() {
    input! {
        l:i64,
        a:i64,
        b:i64,
        m:i64
    };
    let max_v = a+b*(l-1);

    let mut ans = 0 ;
    let mut keta_sum = 0;
    for k in (0..SZ).rev() {
        let k = k as i64;
        let mut v = 1 as i64;
        for _ in 0..k { v*=10 }
        let v = v;
        if max_v<v {continue;}
        let lb = min(max((v-a+b-1)/b,0),l);
        let ub = max(min((v*10-a+b-1)/b,l),0);
        let rate = powm(10,keta_sum,m);
        let x = powm(10,k+1,m);
        let w = ub-lb;
        let sum = rate*((a%m+b%m*(ub-1)%m)%m*f(x,w,m)%m-b%m*g(x,w,m)%m)%m;
        // eprintln!("{} {}",f(x,w,m),g(x,w,m));
        let sum = (sum+m)%m;
        // eprintln!("lbub {} {} {} {}",lb,ub,keta_sum,sum);
        ans = ((ans + sum)%m+m)%m;    
        keta_sum += w*(k+1);
        
    }
    println!("{}",ans);
}