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 Lua (Lua 5.3.5) (judge verdict: Wrong Answer)

local n, a, b, mod = io.read("*n", "*n", "*n", "*n")

local function badd(x, y) return (x + y) % mod end
local function bsub(x, y) return (mod + x - y) % mod end
local function bmul(x, y) return (x * y) % mod end
local function bmul_m(x, y) return (x * y) % (mod - 1) end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res, pow = bmul(res, src), pow - 1
    end
    src, pow = bmul(src, src), pow // 2
  end
  return res
end

local function func(i)
  local c, z = 0, a + b * (i - 1)
  while 0 < z do
    c, z = c + 1, z // 10
  end
  return c
end

local function getright(d, left)
  local min = left
  local max = n
  if d < func(min) then return left - 1 end
  if func(max) == d then return max end
  while 1 < max - min do
    local mid = (min + max) // 2
    if d < func(mid) then max = mid
    else min = mid
    end
  end
  return min
end

local function solve1(d, n)
  if n == 1 then return 1 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + modpow(tend, n - 1)
    n = n - 1
  end
  n = n // 2
  local v = solve1(d, n)
  v = bmul(v, badd(1, modpow(tend, n)))
  return badd(add, v)
end

local function solve2(d, n)
  if n == 1 then return 0 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + bmul(n - 1, modpow(tend, n - 1))
    n = n - 1
  end
  n = n // 2
  local v1 = solve2(d, n)
  v1 = bmul(v1, badd(1, modpow(tend, n)))
  local v2 = solve1(d, n)
  v2 = bmul(v2, bmul(n % mod, modpow(tend, n)))
  return badd(add, badd(v1, v2))
end

local cur_digsum = 0
local left = 1
local ret = 0
for d = 1, 18 do
  local right = getright(d, left)
  if left <= right then
    local s1 = solve1(d, right - left + 1)
    local v1 = bmul(a % mod, s1)
    local v2 = bmul(b % mod, (right - 1) % mod)
    v2 = bmul(v2, s1)
    local v3 = bmul(b % mod, solve2(d, right - left + 1))
    ret = bmul(ret, modpow(10, bmul_m((right - left + 1), d)))
    ret = badd(ret, badd(v1, bsub(v2, v3)))
    cur_digsum = cur_digsum + (right - left + 1) * d
    left = right + 1
  end
  if n < left then break end
end
print(ret)