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<bits/stdc++.h>
#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		//freopen("out.txt", "w", stdout);
	}
}

long long L, A, B;
int mod;

struct node {
  int val[3][3];
};

long long powt[19];
long long cd[19];
node mul(node a, node b)
{
	node c = { 0,0,0,0,0,0,0,0,0 };
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				c.val[i][j] = (c.val[i][j]+1ll*a.val[i][k] * b.val[k][j])%mod;
			}
		}
	}
	return c;
}

node poww(node a, int b)
{
	node res = { 1,0,0,0,1,0,0,0,1 };
	while (b)
	{
		if (b & 1)res = mul(res, a);
		b >>= 1;
		a = mul(a, a);
	}
	return res;
}

int main()
{
	cin >> L >> A >> B >> mod;
	powt[0] = 1;
	for (int i = 1; i <= 18; i++)
		powt[i] = powt[i - 1] * 10ll % mod;

	 long long up = 0;
	 int ans[3] = { 0,A%mod,1 };
	 int temp[3];
	cd[0] = -1;
	for (int d = 1; d <= 18; d++)
	{
		up = up * 10 + 9;
		if (up < A)
		{
			cd[d] = -1;
			continue;
		}
		cd[d] = (up - A) / B;
		cd[d] = min(L-1, cd[d]);
		node p = { powt[d],0,0,1,1,0,0,B%mod,1 };
		p = poww(p, cd[d]-cd[d-1]);
		temp[0] = ((1ll*ans[0] * p.val[0][0] % mod + 1ll * ans[1] * p.val[1][0] % mod) % mod + 1ll * ans[2] * p.val[2][0]) % mod;
		temp[1] = ((1ll * ans[0] * p.val[0][1] % mod + 1ll * ans[1] * p.val[1][1] % mod) % mod + 1ll * ans[2] * p.val[2][1]) % mod;
		temp[2] = ((1ll * ans[0] * p.val[0][2] % mod + 1ll * ans[1] * p.val[1][2] % mod) % mod + 1ll * ans[2] * p.val[2][2]) % mod;
		swap(ans, temp);
	}
	cout << ans[0]%mod << endl;
	return 0;
}