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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>

template <class T, class Int>
T ipow(T b, Int n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

template <class T>
using Vector = std::vector<T>;
template <class T>
using Matrix = Vector<Vector<T>>;

template <class T>
Matrix<T> operator*(const Matrix<T>& a, const Matrix<T>& b) {
    int n = a.size(), m = b.size(), l = b.front().size();
    Matrix<T> ret(n, Vector<T>(l, 0));

    for (int i = 0; i < n; ++i) {
        for (int k = 0; k < l; ++k) {
            for (int j = 0; j < m; ++j) {
                ret[i][k] += a[i][j] * b[j][k];
            }
        }
    }
    return ret;
}

template <class T, class Int>
Matrix<T> mpow(Matrix<T> b, Int n) {
    Matrix<T> ret(b.size(), Vector<T>(b.size(), 0));
    for (int i = 0; i < b.size(); ++i) ret[i][i] = 1;

    while (n > 0) {
        if (n & 1) ret = ret * b;
        n >>= 1;
        b = b * b;
    }
    return ret;
}

struct ModInt {
    using lint = long long;
    using mint = ModInt;

    int val;
    static int MOD;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // assignment
    ModInt& operator=(const ModInt& x) {
        if (this != &x) { this->val = x.val; }
        return *this;
    }

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }
    ModInt operator~() const {
        // non-recursive extended euclidean algorithm
        int s = val, t = MOD;
        mint sx = 1, sy = 0, tx = 0, ty = 1;
        while (s % t != 0) {
            int div = s / t;
            s -= t * div;
            sx -= tx * div;
            sy -= ty * div;

            std::swap(s, t);
            std::swap(sx, tx);
            std::swap(sy, ty);
        }
        return mint(tx);
    }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }

    // compound assignment
    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= ~x; }

    // compare
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};

int ModInt::MOD;
using mint = ModInt;

using lint = long long;

int main() {
    lint l, a, b;
    std::cin >> l >> a >> b >> ModInt::MOD;

    auto ith = [&](lint x) {
        return (x <= a - b ? -1 : (x - a + b - 1) / b);
    };
    // x以上の最小の項

    mint ans = 0;
    for (int k = 1; k <= 18; ++k) {
        lint low = std::max(0LL, ith(ipow(10LL, k - 1))),
             high = std::min(l, ith(ipow(10LL, k)));
        // [low, high)項がk桁
        if (high < 0) continue;
        if (low > l) break;

        lint len = high - low;
        ans *= ipow(ipow(mint(10), k), len);

        Matrix<mint> mat{{ipow(mint(10), k), 1, 0},
                         {0, 1, b},
                         {0, 0, 1}};
        mat = mpow(mat, len);
        ans += mat[0][1] * (a + b * low) + mat[0][2];
    }

    std::cout << ans << std::endl;
    return 0;
}