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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    long L = scanner.nextLong();
    long A = scanner.nextLong();
    long B = scanner.nextLong();
    long M = scanner.nextLong();

    long[][] m = new long[3][3];
    for (int i = 0; i < 2; i++) m[1][i] = 1;
    for (int i = 1; i < 3; i++) m[2][i] = 1;

    long[] res = new long[] {0, A, B};
    long k = 10;
    long l = 0;
    while (A > k) k *= 10;
    while (l < L) {
      m[0][0] = k % M;
      long r = Math.min((k - A - 1) / B, L - 1);
      res = mult(res, modpow(m, r - l + 1, M), M);
      l = r + 1;
      k *= 10;
    }

    System.out.println(res[0]);
  }

  private static long[][] modpow(long[][] m, long c, long M) {
    if (c == 0) return new long[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    if (c == 1) return m;
    if (c % 2 == 0) {
      long[][] m1 = modpow(m, c / 2, M);
      return mult(m1, m1, M);
    } else {
      long[][] m1 = modpow(m, c - 1, M);
      return mult(m1, m, M);
    }
  }

  private static long[] mult(long[] a, long[][] b, long M) {
    long[] r = new long[3];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        r[i] += a[j] * b[j][i] % M;
        if (r[i] >= M) r[i] -= M;
      }
    }
    return r;
  }

  private static long[][] mult(long[][] a, long[][] b, long M) {
    long[][] r = new long[3][3];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          r[i][j] += a[i][k] * b[k][j] % M;
          if (r[i][j] >= M) r[i][j] -= M;
        }
      }
    }
    return r;
  }
}