let rec fold_for a b v f = if a >= b then v else fold_for (a+1) b (f a v) f

let rec pow ( * ) z a n =
  if n = 0 then z
  else if n mod 2 = 1 then a * (pow ( * ) z a (n-1))
  else let h = pow ( * ) z a (n/2) in h * h

let rec intpow = pow ( * ) 1

let i3 = [| [|1; 0; 0|]; [|0; 1; 0|]; [|0; 0; 1|] |]

let () =
  Scanf.scanf "%d %d %d %d" @@ fun l a b modulo ->
  
  let (+@) a b = (a + b + modulo) mod modulo in
  let modmul a b = (a * b) mod modulo in
  let modpow = pow modmul 1 in

  let matmul a b =
    let m = Array.length b in
    Array.init (Array.length a) (fun i ->
    Array.init (Array.length b.(0)) (fun j ->
      fold_for 0 m 0 (fun k s -> s +@ modmul a.(i).(k) b.(k).(j))))
  in

  let rec loop d z =
    if d > 18 then z else
    let i = (intpow 10 (d-1) - a + b - 1) / b |> max 0 in
    let j = (intpow 10 d - a + b - 1) / b |> min l in
    let r = [| [| z; a +@ modmul b i; 1 |] |] in
    let m = [| [| modpow 10 d; 0; 0 |]; [| 1; 1; 0 |]; [| 0; b mod modulo; 1 |]; |] in
    let mc = pow matmul i3 m (j-i) in
    let r' = matmul r mc in
    loop (d+1) r'.(0).(0)
  in
  loop 1 0 |> Printf.printf "%d\n"
