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 Ruby (2.3.3) (judge verdict: Wrong Answer)

#割り算が計算に入るのでそれを排除しようと試行錯誤中
def f(e,a,b)
	if e-a<=0
		return 0
	else
		return (e-a)/b+((e-a)%b>0?1:0)
	end
end

def f2(d,m)
	r=10
	res=1
	while d>0 do
 		if d%2==1
 			res=(res*r)%m
 		end
		r=(r*r)%m
 		d/=2
	end
	return res
end

def f3(r0,p0,m)
	r1=1
	t1=1
	res=0
	r2=1
	while r0>0
		if r0%2==1
			res=(res+t1*r2)%m
		end
		if r1==1
			r1=p0%m
		else
			r1=(r1*r1)%m
		end
		if r0%2==1
			r2=(r2*r1)%m
		end
		t1=(t1*(r1+1))%m
		r0/=2
	end
	return res
end

def f3b(r0,p0,m,base)
	r1=1
	res=0
	t1=1
	k=r0
	d=1
	r2=1
	r3=1
	while r0>0 do
		if r0%2==1
				k=k-r1
				res=(res+t1*r3+(k+base)*f3(r1,p0,m)*r3)%m
		end

		if r2==1
			r2=p0
		else
			r2=(r2*r2)%m
		end
		if r0%2==1
			r3=(r3*r2)%m
		end
		
		t1=(t1*(1+r2)+r1*f3(r1,p0,m))%m
		r1=r1*2
		r0/=2
	end
	return res%m
end

def f4(l,a,b,m)
#コーナーケースがめんどくさいので対策
if l==1
	return a%m
elsif l==2
	return (a+a+b)%m
end

#str=""
#l.times{|i|
#	str=str + (a+b*i).to_s
#}
#ansA=((str.to_i)%m)



rs=(0..22).map{|e| 10**e}
ts=rs.map{|e| f(e,a,b)}.select{|e| e<l}
ts<<l if(ts.last<l)
ra=1
suma=0
suma2=0
suma3=0
sumb=0
sumb2=0
sumc2=0
mr2=1
rk=1
base=l
ts.size.times{|i|
	p1=ts.size-1-i
	p2=p1-1
	if(p2>=0 && ts[p1]>ts[p2])
		p3=rs[p1]
		t3=ts[p1]-ts[p2]-1
		t4=t3+1
		base=base-t4
		q0=f2(p1*(t4+1),m)
		q1=f2(p1*t4,m)
		q2=f2(p1,m)
		q3=(q2+(m-1))%m
		#p ["q",q0,q1,q2,q3]
		#d10=f3((q3*q3)%m,m)
		#d11=f3(q3,m)
		#t10=(((q0-q2)-t4*q3)*d10)%m
		#s10=(((q1+(m-1))%m)*d11)%m
		#t10=(((q0-q2)-t4*q3)/(q3*q3))%m
		s10=f3(t4,q2,m)
		#t10=((q2*s10-t4)/q3)%m
		t10=f3b(t4,q2,m,base-1)
		t11=(s10*(ts[p2]-1))%m
		#p s10
		suma2=((suma2+mr2*s10))%m
		sumb2=((sumb2+mr2*t10))%m
		sumc2=(sumc2+(t4*p1))
		mr2=f2(sumc2,m)%m
		#p [sumb2,base]
	end
}

p ((a*suma2+b*sumb2)%m)
end

ds=gets.split(" ").map{|e| e.to_i}
l=ds[0]
a=ds[1]
b=ds[2]
m=ds[3]

#(1..20).each{|e|
#		p ["ans",e,f3b(e,100,m*m*m,3)]
#}

f4(l,a,b,m)
#[17,44,52].each{|l|
#   [1,4,5,10,25,100,447,8324].each{|a|
#		[3,9,6,5,90,25,34,111,479,9999].each{|b|
#			#p [l,a,b]
#			f4(l,a,b,m*m*m)
#		}
#		
#	}
#}