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>
#include<algorithm>
#include<cstdint>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

struct mint {
  int64_t v_;
  static int64_t Prime;
  mint(int64_t x=0) : v_(x) { normalize(); }
  void normalize() { ((v_ %= Prime) += Prime) %= Prime; }
  mint operator+=(mint const& r){ (v_ += r.v_) %= Prime; return *this; }
  mint operator-=(mint const& r){ (v_ += Prime - r.v_) %= Prime; return *this; }
  mint operator*=(mint const& r){ (v_ *= r.v_) %= Prime; return *this; }
  mint operator+(mint const& r) const { mint res(*this); return res += r; }
  mint operator-(mint const& r) const { mint res(*this); return res -= r; }
  mint operator*(mint const& r) const { mint res(*this); return res *= r; }
  mint pow(int x) const {
    int64_t res = 1, v = v_;
    while(x > 0) {
      if(x&1) (res *= v) %= Prime;
      x/=2; (v *= v) %= Prime;
    }
    return mint(res);
  }
  //mint inv() const { return pow(Prime-2); } not usable
};
int64_t mint::Prime;

template<typename T>
struct affine { //(x, y)|-> (ax+by+e, cx+dy+f)
  T a,b,c,d,e,f;
  affine(): a(T(1)),b(T(0)),c(T(0)),d(T(1)),e(T(0)),f(T(0)) {}
  affine(T const& in_a, T const& in_b, T const& in_c, T const& in_d,
         T const& in_e=0, T const& in_f=0): a(in_a), b(in_b), c(in_c), d(in_d),
    e(in_e), f(in_f) {}
  // (x,y) -> A(*this(x,y)) 
  affine operator*=(affine const& A) {
    T na = A.a*a + A.b*c, nb = A.a*b + A.b*d, ne = A.a*e + A.b*f + A.e;
    T nc = A.c*a + A.d*c, nd = A.c*b + A.d*d, nf = A.c*e + A.d*f + A.f;
    a = na; b = nb; e = ne;
    c = nc; d = nd; f = nf;
    return *this;
  }
  affine operator*(affine const& A) const { affine r(*this); return r *= A; }
  affine pow(int64_t x) {
    affine res, v(*this);
    while(x > 0) {
      if(x&1) res *= v;
      x/=2; v *= v;
    }
    return res;
  }
  void apply(T& x, T& y) const {
    T nx = a*x + b*y + e; 
    y = c*x + d*y + f;
    x = nx;
  }
};

int64_t ceil_div(int64_t x, int64_t y) { // x, y > 0
  return (x+y-1)/y;
}

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  int64_t L, A, B, M; std::cin >> L >> A >> B >> M;
  mint::Prime = M; 
  
  std::vector<int64_t> l(20, 0);
  int64_t b = 0, e = 19;
  for(int64_t d = 1, k = 1; d < 19; ++d, k *= 10) {
    if(A >= k) b = d;
    else {
      l[d] = ceil_div(k-A, B);
      if(l[d] >= L) { e = d; break; }
    }
  }
  l[e] = L;

  mint x, s(A);
  for(int d = b; d < e; ++d) {
    affine<mint> f(mint(10).pow(d), 1, 0, 1, 0, B);
    f.pow(l[d+1] - l[d]).apply(x, s);
  }
  fin(x.v_);
  return 0;
}