Under construction

Still being built. Things may change

Counting Product Subset Coefficients

Time limit2sMemory limit512 MB

Summary
Given huge n (up to 500+ digits) and a prime p, count how many k in [0,n] make the elementary symmetric sum f(n,k) not divisible by p, output the count mod 1e9+7.
Level

Hard8 of 10

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

Problem

For integers nn and kk, let [n]={1,2,…,n}[n]=\{1,2,\dots,n\}. The value f(n,k)f(n,k) is the sum, over every kk-element subset of [n][n], of the product of the elements in that subset. In other words,

[ f(n,k)=\sum_{S\subseteq [n],\ |S|=k}\prod_{x\in S}x. ]

Also, f(n,0)=1f(n,0)=1.

Given a positive integer nn and a prime pp, count how many integers kk with 0≤k≤n0\le k\le n make f(n,k)f(n,k) not divisible by pp.

Input

The first line contains an integer nn and a prime pp, separated by a space.

Output

Print the number of valid values of kk, modulo 109+710^9+7.

Constraints

  • 1≤n<105011 \le n < 10^{501}
  • 2≤p≤1052 \le p \le 10^5
  • pp is prime

Examples1

  1. Example 1

    Input
    4 2
    
    Expected output
    2