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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using static System.Console;
using static Func;
class Z { static void Main() => new K(); }
class K
{
	long[] GL => ReadLine().Split().Select(long.Parse).ToArray();
	readonly int M;
	readonly long[] pow10 = new long[19];
	public K()
	{
		pow10[0] = 1;
		for (var i = 1; i <= 18; i++) pow10[i] = pow10[i - 1] * 10;
		var I = GL;
		long L = I[0], A = I[1], B = I[2];
		P.M = Matrix.Mod = M = (int)I[3];
		var l = new List<long> { 0 };
		while (l.Last() < L)
		{
			var k = (A + B * l.Last()).ToString().Length;
			l.Add(FirstBinary(l.Last(), L, _j => A + B * _j >= pow10[k]));
		}
		var ans = new P(0, 1);
		for (var i = 1; i < l.Count; i++) ans = ans.Combine(Solve(l[i] - l[i - 1], A + B * l[i - 1], B));
		WriteLine(ans.Mod);
	}
	// A ~ A+B*(L-1) まで全て k 桁
	P Solve(long L, long A, long B)
	{
		var k = A.ToString().Length;
		var mat = new Matrix(3);
		mat[0, 0] = pow10[k];
		mat[0, 1] = mat[1, 1] = mat[2, 2] = 1;
		mat[1, 2] = B;
		mat = mat.Pow(L);
		return new P((mat[0, 1] * (A % M) % M + mat[0, 2]) % M, Pow(Pow(10, k, M), L, M));
	}
}
struct P
{
	public static int M;
	public readonly long Mod;
	public readonly long Pow10;
	public P(long m, long l) { Mod = m; Pow10 = l; }
	public P Combine(P p) => new P((Mod * p.Pow10 % M + p.Mod) % M, Pow10 * p.Pow10 % M);
}
static class Func
{
	public static long FirstBinary(long min, long max, Predicate<long> pred)
	{
		while (min < max)
		{
			var mid = (min + max) / 2;
			if (pred(mid)) max = mid;
			else min = mid + 1;
		}
		return min;
	}
	public static long Pow(long a, long b, long mod)
	{
		var p = 1L;
		var x = a;
		while (b > 0)
		{
			if ((b & 1) == 1) p = (p * x) % mod;
			b >>= 1;
			x = (x * x) % mod;
		}
		return p;
	}
}
class Matrix
{
	public static int Mod;
	public int Size { get; protected set; }
	readonly long[,] a;
	public long this[int r, int c] { get { return a[r, c]; } set { a[r, c] = value % Mod; } }
	public Matrix(int r) { Size = r; a = new long[r, r]; }
	public Matrix(int r, Func<int, int, long> f) : this(r) { for (var i = 0; i < r; i++) for (var j = 0; j < r; j++) a[i, j] = f(i, j) % Mod; }
	public static Matrix operator *(Matrix p, Matrix q)
		=> new Matrix(p.Size, (i, j) => { var sum = 0L; for (var k = 0; k < p.Size; k++) sum = (sum + p.a[i, k] * q.a[k, j] % Mod) % Mod; return sum; });
	public static Matrix One(int size) => new Matrix(size, (i, j) => i == j ? 1 : 0);
	public Matrix Pow(long n)
	{
		var p = One(Size);
		var x = this;
		while (n > 0)
		{
			if ((n & 1) == 1) p *= x;
			n >>= 1;
			x *= x;
		}
		return p;
	}
}