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)

// https://atcoder.jp/contests/abc129/tasks/abc129_f
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

#[derive(Debug, Clone)]
struct SquareMatrix {
    n: usize,
    data: Vec<Vec<i64>>,
    modulo: i64
}

impl SquareMatrix {
    fn new(data: Vec<Vec<i64>>, modulo: i64) -> SquareMatrix {
        let n = data.len();
        SquareMatrix { n: n, data: data, modulo: modulo }
    }

    fn eye(n: usize, modulo: i64) -> SquareMatrix {
        let mut data = dvec!(0; n, n);
        for i in 0..n {
            data[i][i] = 1;
        }
        SquareMatrix { n: n, data: data, modulo: modulo }
    }

    fn mul(u: i64, v: i64, modulo: i64) -> i64 {
        u * v % modulo
    }

    fn add(u: i64, v: i64, modulo: i64) -> i64 {
        (u + v) % modulo
    }

    fn multiply(left: &SquareMatrix, other: &SquareMatrix) -> SquareMatrix {
        assert!(left.n == other.n);
        assert!(left.modulo == other.modulo);
        let modulo = left.modulo;
        let n = left.n;
        let mut data = dvec!(0; n, n);
        for i in 0..n {
            for j in 0..n {
                let mut w = 0;
                for k in 0..n {
                    w = Self::add(w, Self::mul(left.data[i][k], other.data[k][j], modulo), modulo);
                }
                data[i][j] = w;
            }
        }
        SquareMatrix { n: n, data: data, modulo: modulo }
    }

    fn pow(self, mut p: i64) -> SquareMatrix {
        let n = self.n;
        let mut ret = Self::eye(n, self.modulo);
        let mut a = self;
        while p >= 1 {
            if p & 1 == 1 {
                ret = Self::multiply(&ret, &a);
            }
            a = Self::multiply(&a, &a);
            p >>= 1;
        }
        ret
    }
}

fn indices(from: i64, to: i64, a: i64, b: i64) -> (i64, i64) {
    let from = max(from, a);
    if to < a {
        return (-1, -1);
    }
    let mut begin_idx = (from - a) / b + ifv!((from - a) % b == 0, 0, 1);
    if from <= a {
        begin_idx = 0;
    }
    let end_idx = (to - a) / b;
    (begin_idx, end_idx)
}

struct Combination {
    fact: Vec<u64>,
    invfact: Vec<u64>,
}

fn pow(a: i64, p: i64, modulo: i64) -> i64 {
    let mut p = p;
    let mut aa = a % modulo;
    let mut ret = 1;
    while p >= 1 {
        if p & 1 == 1 {
            ret *= aa;
            ret %= modulo;
        }
        p >>= 1;
        aa = aa * aa % modulo;
    }
    ret
}

fn build_dbg(a: i64, b: i64, from_index: i64, to_index: i64, keta: i64, modulo: i64) -> i64 {
    let mut first_num = a + from_index * b;
    let last_num = a + to_index * b;
    let keta = pow(10, keta, modulo);
    let mut ret = 0;
    while first_num <= last_num {
        ret *= keta;
        ret += first_num;
        ret %= modulo;
        first_num += b;
    }
    ret
}

fn build(a: i64, b: i64, from_index: i64, to_index: i64, keta: i64, modulo: i64) -> i64 {
    let first_num = (a % modulo + from_index % modulo * b % modulo) % modulo;
    let b = b % modulo;
    let pw = pow(10, keta, modulo);
    let sqsum = vec![
        vec![pw, 1, 0, 0],
        vec![0,  1, 0, 0],
        vec![1, 0,  1, 0],
        vec![0, 1,  0, 1]
    ];
    let mat1 = SquareMatrix::new(sqsum, modulo);
    let pmat = mat1.pow(to_index-from_index+1);

    let one = pmat.data[2][0] % modulo;
    let onetwo = pmat.data[2][1] % modulo;
    (one * first_num + onetwo * b) % modulo
}

fn main() {
    input! {
        l: i64, a: i64, b: i64, modulo: i64
    };

    // debug!(
    //     build_dbg(10000000000007, 1000000000000007, 100, 106, 18, 998244353),
    //     build(10000000000007, 1000000000000007, 100, 106, 18, 998244353)
    // );

    let mut indice_pairs = vec![];
    let mut keta = 1;
    let mut knum = 1;
    while keta < 1e18 as i64 {
        let (f, t) = indices(keta, keta*10-1, a, b);
        if f != -1 && f <= l-1 && f <= t {
            let t = min(t, l-1);
            indice_pairs.push((f, t, knum));
        }
        knum += 1;
        keta *= 10;
    }
    indice_pairs.reverse();
    // debug!(indice_pairs);

    let mut total = 0;
    let mut left_keta = 0;
    for (f, t, kn) in indice_pairs {
        let sz = (t-f+1)%(modulo-1)*kn%(modulo-1);
        total += build(a, b, f, t, kn, modulo) * pow(10, left_keta, modulo) % modulo;
        total %= modulo;
        left_keta += sz%(modulo-1);
        left_keta %= (modulo-1);
        debug!(left_keta);
    }
    println!("{}", total);
}