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: Time Limit Exceeded)

import java.io.*;
import java.util.*;
import java.math.BigInteger;
  
public class Main {
	
	FastScanner _in;
	PrintWriter _out;
 
	void solve() {
		long l = _in.nextLong();
		long a = _in.nextLong();
		long b = _in.nextLong();
		long m = _in.nextLong();
		String s = String.valueOf(a);
		String inc = String.valueOf(b);
		BigInteger x = new BigInteger(s);
		BigInteger y = new BigInteger(inc);
		BigInteger z;
		BigInteger mod = new BigInteger(String.valueOf(m));
		
		long i;
		
		for (i = 1; i < l; i++) {
		    z = x.add(y);
		    s = s + z.toString();
		    x = z;
		}
		
		z = new BigInteger(s);
		x = z.mod(mod);
		
		_out.println(x.toString());
	}
	
	void run() throws Exception {
		_in = new FastScanner();
		_out = new PrintWriter(System.out);
 
		solve();
		
		_out.close();
	}
 
	public static void main(String[] args) throws Exception {
		new Main().run();
	}
	
	class FastScanner {
		BufferedReader _br;
		StringTokenizer _st;
		
		public FastScanner(String fileName) {
			try {
				_br = new BufferedReader(new FileReader(fileName));
			} catch (FileNotFoundException e) {
				// Error
			}
		}
		
		public FastScanner() {
			_br = new BufferedReader(new InputStreamReader(System.in));
		}
		
		String nextToken() {
			while (_st == null || !_st.hasMoreElements()) {
				try {
					_st = new StringTokenizer(_br.readLine());
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			
			return _st.nextToken();
		}
		
		String next() {
			return nextToken();
		}
		
		short nextShort() {
			return Short.parseShort(nextToken());
		}
		
		int nextInt() {
			return Integer.parseInt(nextToken());
		}
		
		long nextLong() {
			return Long.parseLong(nextToken());
		}
		
		float nextFloat() {
			return Float.parseFloat(nextToken());
		}
		
		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}
 
}