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: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn mul(a: &[[usize; 3]; 3], b: &[[usize; 3]; 3], m: usize) -> [[usize; 3]; 3] {
    let mut c = [[0; 3]; 3];
    for i in 0..3 {
        for j in 0..3 {
            for k in 0..3 {
                c[i][j] = (c[i][j] + a[i][k] * b[k][j] % m) % m;
            }
        }
    }
    c
}

fn pow(a: &[[usize; 3]; 3], mut p: usize, m: usize) -> [[usize; 3]; 3] {
    let mut c = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
    let mut d = a.clone();
    while p > 0 {
        if p % 2 == 1 {
            c = mul(&c, &d, m);
        }
        d = mul(&d, &d, m);
        p /= 2;
    }
    c
}

fn main() {
    input! {
        l: usize,
        a: usize,
        b: usize,
        m: usize,
    }
    let s_max = a + b * (l - 1);
    let n = (s_max as f64).log10() as usize + 1;
    let mut d = vec![0; n];
    d[0] = if a <= 9 {
        (min(s_max, 9) - a) / b + 1
    } else {
        0
    };
    let mut f = d[0];
    for i in 1..n {
        let e = 10usize.pow(i as u32 + 1) - 1;
        d[i] = if a <= e {
            (min(s_max, e) - a) / b + 1 - f
        } else {
            0
        };
        f += d[i];
    }
    for i in 0..n {
        if d[i] > 0 {
            d[i] -= 1;
            break;
        }
    }

    let mut s = (a % m, (a + b) % m);
    for i in 0..n {
        if d[i] == 0 {
            continue;
        }
        let e = 10usize.pow(i as u32 + 1) % m;
        let x = pow(&[[e, 0, 0], [1, 1, 0], [0, b, 1]], d[i], m);
        s = (
            (x[0][0] * s.0 % m + x[1][0] * s.1 % m + x[2][0]) % m,
            (x[0][1] * s.0 % m + x[1][1] * s.1 % m + x[2][1]) % m,
        );
    }
    println!("{:?}", s.0);
}
*/

fn main() {
    let exe = "/tmp/binDC26C3E6";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";