Under construction

Still being built. Things may change

Harmonic Series Modulo a Prime

Interview

Time limit4sMemory limit1024 MB

Summary
Given a prime p and a range l to r, compute the sum of the modular inverses of each i in the range, modulo p.
Level

Medium5 of 10

Topics
Number theory, Math, Prefix sum
Solved
No attempts yet

Statement

In mathematics, the harmonic series is the following sum:

∑k=1∞1k=11+12+13+14+…\sum_{k=1}^{\infty} \frac{1}{k} = \frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \ldots

The series has many remarkable properties. It diverges, and the sum of its first nn terms approaches ln⁡(n)+γ\ln(n) + \gamma as nn goes to infinity, where γ\gamma is the Euler-Mascheroni constant.

Tor does not care much for mathematical analysis, so the remarkable properties of the harmonic series do not interest him much. Out of boredom, Tor invented a new mathematical object and named it the harmonic series modulo a prime pp.

Tor defines the harmonic series modulo a prime pp as the following sum:

∑i=1p−11i(modp)=(11+12+…+1p−2+1p−1)(modp)\sum_{i=1}^{p-1} \frac{1}{i} \pmod p = \left(\frac{1}{1} + \frac{1}{2} + \ldots + \frac{1}{p-2} + \frac{1}{p-1}\right) \pmod p

For an integer x∈[1;p−1]x \in [1; p-1], Tor calls 1x\frac{1}{x} the integer y∈[1;p−1]y \in [1; p-1] such that x⋅y≡1(modp)x \cdot y \equiv 1 \pmod p. If pp is prime, then for every such xx there is exactly one suitable yy, and this can be proven.

Tor wants to know the value of the sum of terms from ll to rr, that is, ∑i=lr1i(modp)\sum_{i=l}^{r} \frac{1}{i} \pmod p.

Input

The first line contains three integers ll, rr, and pp (1≤l≤r<p1 \le l \le r < p, r−l<2⋅107r - l < 2 \cdot 10^7, p≤109p \le 10^9, pp is prime).

Output

Print one integer, the answer to the problem. All arithmetic operations in this problem are performed modulo pp.

Hint

A natural number xx is prime if x≠1x \ne 1 and xx has no divisors other than 11 and xx.

Examples2

  1. Example 1

    Input
    1 5 7
    
    Expected output
    1
    
  2. Example 2

    Input
    3 10 31
    
    Expected output
    4