This page is still under construction.

Parts of this page are still being built. What you see may change.

Drink with Double or Fold

Time limit1sMemory limit512 MB

Summary
Each term from position k+1 on is the sum of the previous k terms mod P; find the N-th term with N up to 1e9.
Level

Medium7 of 10

Topics
Matrix, Math, Dynamic programming, Divide and conquer
Solved
No attempts yet

Problem

Youngsu and his N−1N-1 friends invented a new drinking game called "Drink with Double or Fold". The game proceeds as follows.

  1. At the start of the game, the first, second, ⋯\cdots, kk-th person each decide how much to drink.
  2. Starting from the ii-th person (i≥k+1i \geq k+1), that person drinks an amount equal to the sum of what the i−1,i−2,⋯ ,i−ki-1, i-2, \cdots, i-k-th people drank, taken modulo PP.

Given the amounts a1,a2,⋯ ,aka_1, a_2, \cdots, a_k that the first kk people drink, and given that Youngsu drinks last, find the amount of alcohol Youngsu will drink. Youngsu and his friends have no limit on how much they can drink, so there is no need to worry about their health.

Input

The first line gives nn and kk. (k<N≤109k < N \leq 10^9, 1≤k≤1001 \leq k \leq 100)

The second line gives the amounts a1,a2,⋯ ,aka_1, a_2, \cdots, a_k that the first kk people drink, in order. (1≤ai≤1091 \leq a_i \leq 10^9)

The last line gives the integer PP. (1≤P≤109+71 \leq P \leq 10^9+7)

Output

Print the amount of alcohol Youngsu will drink.

Examples1

  1. Example 1

    Input
    5 3
    1 2 3
    17
    
    Expected output
    11