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
		if (e-a)%b==0
			return ((e-a)/b)
		else
			return ((e-a)/b)+1
		end
	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(a, m)
            x = 0
            y = 1
            mgcd = m;
            px = 1
            py = 0
            pgcd = a;
            temp = 0;
            while (mgcd > 0) do
                quotient = pgcd / mgcd;

                temp = x;
                x = px - quotient * temp;
                px = temp;

                temp = y;
                y = py - quotient * temp;
                py = temp;

                temp = mgcd;
                mgcd = pgcd - quotient * temp;
                pgcd = temp;
            end
            return px;
end

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

rs=(0..22).map{|e| 10**e}
ts=rs.map{|e| f(e,a,b)}.select{|e| e<=l}
ts<<(l)
ra=1
suma=0
suma2=0
sumb=0
sumb2=0
sumc2=0
mr2=1
#p ts
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]
		t4=t3-1
		q0=f2(p1*t3,m)
		q1=f2(p1*t3+p1,m)
		q2=f2(p1,m)
		q3=(q2+(m-1))%m
		q4=(t4*q3)%m
		m10=f3((q3*q3)%m,m)
		m11=f3(q3,m)
		t10=((((q1+(m-q2)+(m-(t3*q3)%m)))%m)*m10)%m
		t11=(((((q0+(m-1))%m)*m11)%m)*(ts[p2]-1))%m
		s10=(((q0+(m-1))%m)*m11)%m
		suma2=((suma2+mr2*s10))%m
		sumb2=((sumb2+mr2*(t10+t11)))%m
		#t3.times{|e|
		#	suma=(suma+ra)%m
		#	sumb=(sumb+ra*l)%m
		#	ra=(ra*p3)%m
		#	l-=1
		#}
		#p [suma,suma2,sumb,sumb2]
		sumc2=(sumc2+(t3*p1))%m
		mr2=f2(sumc2,m)%m
	end
}
#p ((a*suma+b*sumb)%m)
p ((a*suma2+b*sumb2)%m)