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 Go (1.6) (judge verdict: Wrong Answer)

package main
import (
  "fmt"
  "strconv"
)
var mod int64
func main() {
  var l,a,b,m int64
  fmt.Scan(&l,&a,&b,&m)
  mod = m
  c := make([]int64,19)
  t := int64(1)
  k := len(strconv.FormatInt(a,10))
  for d:=0;d<k;d++ { t *= 10 }
  for d:=k;d<19;d++ {
    if t-1 >= a+b*(l-1) { c[d] = l;break }
    c[d] = (t-a+b-2)/b
    t *= 10
  }
  for d:=18;d>0;d-- {
    if c[d] > 0 { c[d] -= c[d-1] }
  }
  t = 1
  y := Identity(3)
  for d:=1;d<19;d++ {
    t = (10*t)%mod
    z := NewMatrix(3)
    z[0].v[0] = t
    z[1].v[0] = 1
    z[1].v[1] = 1
    z[2].v[1] = b
    z[2].v[2] = 1
    y = y.MatMul(z.Pow(c[d]))
  }
  x := Vector{[]int64{0,a,1}}
  fmt.Println(x.DotProduct(y.ColVec(0)))
}
type Vector struct { v []int64 }
func(x Vector) DotProduct(y Vector) int64 {
  if len(x.v) != len(y.v) || len(x.v) == 0 { return 0 }
  d := (x.v[0]*y.v[0])%mod
  for i:=1;i<len(x.v);i++ { d = (d+x.v[i]*y.v[i])%mod }
  return d
}
type Matrix []Vector
func NewMatrix(n int) Matrix {
  m := Matrix{}
  for i:=0;i<n;i++ { m = append(m,Vector{make([]int64,n)}) }
  return m
}
func Identity(n int) Matrix {
  e := NewMatrix(n)
  for i:=0;i<n;i++ { e[i].v[i] = 1 }
  return e
}
func(a Matrix) ColVec(j int) Vector {
  if len(a) == 0 || j >= len(a[0].v) { return Vector{nil} }
  v := make([]int64,len(a))
  for i:=0;i<len(a);i++ { v[i] = a[i].v[j] }
  return Vector{v}
}
func(a Matrix) Transpose() Matrix {
  if len(a) == 0 || len(a[0].v) == 0 { return Matrix{Vector{nil}} }
  m := Matrix{}
  for j:=0;j<len(a[0].v);j++ { m = append(m,a.ColVec(j)) }
  return m
}
func(a Matrix) VecMul(x Vector) Vector {
  if len(a) == 0 || len(a[0].v) != len(x.v) ||
  len(x.v) == 0 { return Vector{nil} }
  v := make([]int64,len(a))
  for i:=0;i<len(a);i++ { v[i] = a[i].DotProduct(x) }
  return Vector{v}
}
func(a Matrix) MatMul(b Matrix) Matrix {
  if len(a) == 0 || len(a[0].v) == 0 || len(b) == 0 || len(b[0].v) == 0 ||
  len(a[0].v) != len(b) { return Matrix{Vector{nil}} }
  m := Matrix{}
  for j:=0;j<len(b[0].v);j++ { m = append(m,a.VecMul(b.ColVec(j))) }
  return m.Transpose()
}
func(a Matrix) Pow(n int64) Matrix {
  if len(a) == 0 || len(a[0].v) == 0 ||
  len(a) != len(a[0].v) { return Matrix{Vector{nil}} }
  if n == 0 { return Identity(len(a)) }
  if n%2 == 0 { c := a.Pow(n/2);return c.MatMul(c) }
  return a.MatMul(a.Pow(n-1))
}