Under construction

Still being built. Things may change

Stirling Number

Time limit9sMemory limit256 MB

Summary
Given n, l, r, and a prime p, compute the sum of unsigned Stirling numbers of the first kind [n, k] for k from l to r, modulo p.
Level

Hard8 of 10

Topics
Number theory, Combinatorics, Divide and conquer
Solved
No attempts yet

Problem

The Stirling number of the first kind [nk]\begin{bmatrix} n \\ k \end{bmatrix} is the number of permutations of nn elements with exactly kk disjoint cycles. The well-known recurrence is

[n+1k]=n[nk]+[nk−1]\begin{bmatrix} n+1 \\ k \end{bmatrix} = n \begin{bmatrix} n \\ k \end{bmatrix} + \begin{bmatrix} n \\ k-1 \end{bmatrix}

for k>0k>0, with the initial conditions

[00]=1and[n0]=[0n]=0\begin{bmatrix} 0 \\ 0 \end{bmatrix} = 1 \quad \text{and} \quad \begin{bmatrix} n \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ n \end{bmatrix} = 0

for n>0n>0.

Given four integers nn, ll, rr, and pp, find the value of

(∑k=lr[nk]) mod p\left(\sum_{k=l}^{r}{\begin{bmatrix} n \\ k \end{bmatrix}} \right) \bmod p

where pp is prime.

Input

The first line contains four integers nn, ll, rr, and pp (1≤n≤10181 \le n \le 10^{18}, 0≤l≤r≤n0 \le l \le r \le n, 2≤p≤1062 \le p \le 10^6, pp is prime).

Output

Output an integer denoting the answer.

Examples3

  1. Example 1

    Input
    4 1 4 5
    
    Expected output
    4
    
  2. Example 2

    Input
    6 5 5 29
    
    Expected output
    15
    
  3. Example 3

    Input
    1000 685 975 999983
    
    Expected output
    482808