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>
using namespace std;

unsigned int MOD = 1e9 + 7;

unsigned long long L, A, B, M;

int main()
{
	cin >> L >> A >> B >> M;

	unsigned long long s = 0;

	int c = 0;
	for( int i = L - 1; i >= 0; i-- )
	{
		unsigned long long l = A + B * i;

		//cout << l << " * 10^" << c << " % " << M << " = ";

		unsigned long long b = l;
		unsigned long long v = 1;
		unsigned long long m = 1 << 30;

		while( (c & m) == 0 && m > 0 )
		{
			m >>= 1;
		}

		if( c > 0 )
		{
			unsigned long long k = m;
			while( k > 0 )
			{
				v *= v;
				v %= M;
				if( (c & k) > 0 ) v *= 10;
				v %= M;
				k >>= 1;
			}
		}
		b %= M;

		//cout << b * v << "\n";

		s += b * v;
		s %= M;

		for( ; l > 0; l /= 10 ) c++;
	}

	cout << s << "\n";

	return 0;
}