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: Wrong Answer)

#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <string>
#include <numeric>
#include <tuple>

template <class Tp>
Tp gcd(Tp a, Tp b, Tp& x, Tp& y) {
  x = Tp(0);
  y = Tp(1);
  for (Tp u = y, v = x; a;) {
    Tp q = b/a;
    std::swap(x -= q*u, u);
    std::swap(y -= q*v, v);
    std::swap(b -= q*a, a);
  }
  return b;
}

template <class Tp>
Tp modinv(Tp a, Tp mod) {
  Tp x, y;
  gcd(a, mod, x, y);
  x %= mod;
  if (x < 0) x += mod;
  return x;
}

template <class Tp>
Tp modadd(Tp a, Tp b, Tp mod) {
  a += b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <class Tp>
Tp modadd(const std::initializer_list<Tp>& adds, Tp mod) {
  Tp res = 0;
  for (const auto& add: adds) {
    res += add % mod;
    if (res < 0) res += mod;
    if (res >= mod) res -= mod;
  }
  return res;
}

template <class Tp>
Tp modsub(Tp a, Tp b, Tp mod) {
  a -= b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <class Tp>
Tp modmul(const std::initializer_list<Tp>& muls, Tp mod) {
  Tp res = 1;
  for (const auto& mul: muls) (res *= mul) %= mod;
  return res;
}

template <class Tp>
Tp modpow(Tp base, intmax_t iexp, Tp mod) {
  Tp res = 1;
  for (Tp dbl = base; iexp; iexp >>= 1) {
    if (iexp & 1) res = res * dbl % mod;
    dbl = dbl * dbl % mod;
  }
  return res;
}

intmax_t nya(intmax_t k, intmax_t a, intmax_t d, intmax_t m, intmax_t p) {
  // fprintf(stderr, "(%jd %jd %jd %jd %jd)\n", k, a, d, m, p);
  intmax_t ten = 10;
  intmax_t A = (modpow(ten, k*m, p)-1) * modinv(modpow(ten, k, p)-1, p);
  if ((A %= p) < 0) A += p;
  (A *= a % p) %= p;
  // fprintf(stderr, "A: %jd\n", A);

  intmax_t D = (modpow(ten, k*m, p) + m-1 - modpow(ten, k, p)*m % p) % p;
  intmax_t tmp = (modpow(ten, k, p) + p-1) % p;
  (D *= modinv(tmp*tmp%p, p)) %= p;
  // fprintf(stderr, "D: %jd\n", D);
  if ((D %= p) < 0) D += p;
  (D *= d % p) %= p;

  return (A + D) % p;
}

int main() {
  intmax_t L, A, B, M;
  scanf("%jd %jd %jd %jd", &L, &A, &B, &M);

  intmax_t tenpow = 10;
  std::vector<intmax_t> res;
  intmax_t a = A;
  intmax_t d = B;
  intmax_t k = 1;
  while (L > 0) {
    if (a >= tenpow) {
      tenpow *= 10;
      ++k;
      continue;
    }

    intmax_t m = L;
    if (a + (L-1)*d >= tenpow) {
      m = (tenpow-a + d-1) / d;
    }

    // fprintf(stderr, "m: %jd\n", m);

    intmax_t curten = modpow(tenpow % M, m, M);
    for (auto& ri: res) (ri *= curten) %= M;

    res.push_back(nya(k, a, d, m, M));

    L -= m;
    if (L == 0) break;
    a += m*d;
    tenpow *= 10;
    ++k;
  }

  intmax_t y = 0;
  for (auto ri: res) (y += ri) %= M;
  printf("%jd\n", y);
}