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.15.1) (judge verdict: Wrong Answer)

// Practice AtCoder
// author: Leonardone @ NEETSDKASU
// 解説読後

fn main() {
    let mut stdin = String::new();
    std::io::Read::read_to_string(
        &mut std::io::stdin(),
        &mut stdin).unwrap();
    let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
        ($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    
    let l = get!();
    let a = get!();
    let b = get!();
    let m = get!();
    
    let mut d = 1;
    let mut c = 0;
    let mut x = 0;
    let mut s = a;
    for i in 0..18 {
    	d *= 10;
        let t = bin(a, b, d);
        let cd = std::cmp::min(l, t) - c;
        let v = vec![d % m, 0, 0, 1, 1, 0, 0, b, 1];
        let w = pow(&v, cd, m);
        let z = vec![x, s, 1, 0, 0, 0, 0, 0, 0];
        let e = mul(&z, &w, m);
        x = e[0];
        s = e[1];
        // println!("{} {} {} {} {} {}", i+1, c, t, cd, x, s);
        c = t;
        if c >= l {
        	break;
        }
    }
    println!("{}", x);
}

fn bin(a: i64, b: i64, d: i64) -> i64 {
	let mut lp = 0;
    let mut rp = (1e18 as i64 - a + b - 1) / b;
    while lp < rp {
    	let hp = (rp - lp) / 2 + lp;
        let v = a + b * hp;
        if v < d {
        	lp = hp + 1;
        } else {
        	rp = hp;
        }
    }
    rp
}

fn mul(a: &Vec<i64>, b: &Vec<i64>, m: i64) -> Vec<i64> {
	let mut ret = vec![0; 9];
    for y in 0..3 {
    	for x in 0..3 {
        	let mut r = 0;
            for i in 0..3 {
            	r += a[y*3+i] * b[i*3+x] % m;
                r %= m;
            }
            ret[y*3+x] = r;
        }
    }
    ret
}


fn pow(v: &Vec<i64>, p: i64, m: i64) -> Vec<i64> {
	if p == 0 {
    	vec![1, 0, 0, 0, 1, 0, 0, 0, 1]
    } else {
    	let t = v.clone();
        if p == 1 {
        	t
    	} else if p % 2 == 0 {
    		pow(&mul(v, &t, m), p / 2, m)
		} else {
        	mul(&t, &pow(v, p - 1, m), m)
        }
    }
}