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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cassert>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

ll MOD;
struct ModInt {
    ll x;
    ModInt (ll a) : x((a % MOD + MOD) % MOD) {}
    ModInt operator- () const {
        return ModInt(-x);
    }
    ModInt &operator+= (const ModInt &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    ModInt &operator-= (const ModInt &a) {
        if ((x -= a.x) < 0) x += MOD;
        return *this;
    }
    ModInt &operator*= (const ModInt &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    ModInt operator+ (const ModInt &a) const {
        ModInt res(*this);
        return res += a;
    }
    ModInt operator- (const ModInt &a) const {
        ModInt res(*this);
        return res -= a;
    }
    ModInt operator* (const ModInt &a) const {
        ModInt res(*this);
        return res *= a;
    }
    ModInt pow(ll t) const {
        if (!t) return 1;
        ModInt a = pow(t / 2);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    ModInt inv() const {
        return pow(MOD - 2);
    }
    ModInt &operator/= (const ModInt &a) {
        return (*this) *= a.inv();
    }
    ModInt operator/ (const ModInt &a) const {
        ModInt res(*this);
        return res /= a;
    }
    friend istream &operator>> (istream &is, ModInt &a) {
        is >> a.x;
        return is;
    }
    friend ostream &operator<< (ostream &os, const ModInt &a){
        os << a.x;
        return os;
    }
};

template <typename T>
struct SquareMatrix {
    int L;
    vector<vector<T>> M;
    T e, f;
    SquareMatrix(int n, const T &x, const T &y) : L(n), M(n, vector<T>(n, x)), e(x), f(y) {}
    const vector<T> &operator[](int k) const {
        return M[k];
    }
    vector<T> &operator[](int k) {
        return M[k];
    }
    SquareMatrix E() {
        SquareMatrix<T> Mat(L, e, f);
        REP(i, L) Mat[i][i] = f;
        return Mat;
    }
    SquareMatrix &operator+=(const SquareMatrix &x) {
        REP(i, L) REP(j, L) (*this)[i][j] += x[i][j];
        return (*this);
    }
    SquareMatrix &operator-=(const SquareMatrix &x) {
        REP(i, L) REP(j, L) (*this)[i][j] -= x[i][j];
        return (*this);
    }
    SquareMatrix &operator*=(const SquareMatrix &x) {
        SquareMatrix res(L, e, f);
        REP(i, L) REP(j, L) REP(k, L) res[i][j] += ((*this)[i][k] * x[k][j]);
        swap((*this).M, res.M);
        return (*this);
    }
    SquareMatrix &operator^=(ll n) {
        SquareMatrix res(L, e, f);
        REP(i, L) res[i][i] = f;
        while (n) {
            if (n & 1) res *= (*this);
            (*this) *= (*this);
            n >>= 1;
        }
        swap((*this).M, res.M);
        return (*this);
    }
    SquareMatrix operator+(const SquareMatrix &x) const {
        return SquareMatrix(*this) += x;
    }
    SquareMatrix operator-(const SquareMatrix &x) const {
        return SquareMatrix(*this) -= x;
    }
    SquareMatrix operator*(const SquareMatrix &x) const {
        return SquareMatrix(*this) *= x;
    }
    SquareMatrix operator^(ll n) const {
        return SquareMatrix(*this) ^= n;
    }
};

int main() {
    ll L, A, B, M;
    cin >> L >> A >> B >> M;
    
    MOD = M;
    vector<ll> T(19, 1);
    REP(i, 18) T[i + 1] = T[i] * 10;
    ModInt TEN(10);
    ModInt ans(0);
    REP(i, 18) {
        ll p = (T[i] - A + B - 1) / B, q = (T[i + 1] - A + B - 1) / B;
        p = max(0LL, p); q = max(0LL, q);
        p = min(L, p); q = min(L, q);
        ans *= TEN.pow((unsigned long long)(q - p) * (unsigned long long)(i + 1));
        SquareMatrix<ModInt> Mat(3, ModInt(0), ModInt(1));
        Mat[0][0] = ModInt(T[i + 1]);
        Mat[0][1] = ModInt(1);
        Mat[1][1] = ModInt(1);
        Mat[1][2] = ModInt(1);
        Mat[2][2] = ModInt(1);
        Mat ^= (q - p);
        ans += ModInt(A + B * p) * Mat[0][1] + ModInt(B) * Mat[0][2];
    }
    cout << ans << "\n";
    return 0;
}