#include <iostream>
#include <vector>
using namespace std;
int mod, np;
long long p[ 32010 ] ;
class Powerit
{
public :
Powerit( ) { }
long long mul( long long x, long long k)
{
if ( k == 1 ) return x;
long long kk = mul( x, k/ 2 ) ;
if ( k % 2 == 0 )
return ( ( kk * kk) % mod) ;
else
return ( ( ( ( kk * kk) % mod) * x) % mod) ;
}
int calc( int n, int k, int m)
{
mod = m;
int cp = m;
// EFT with m
int x = 2 ;
while ( x * x <= cp) {
long long kk = 1 ;
while ( m % x == 0 )
{
kk* = x;
m = m/ x;
}
if ( kk > 1 )
{
kk = ( kk / x) * ( x - 1 ) ;
np++ ;
p[ np] = kk;
}
x++ ;
}
if ( m > 1 )
{
np++ ;
p[ np] = m;
}
m = cp;
long long kk = 1 ;
for ( int i = 1 ; i <= np; i++ )
kk * = p[ i] ;
// 2^k - 1
long long mu = 1 ;
for ( int i = 1 ; i <= k; i++ )
mu = ( mu * 2 ) % kk;
mu = ( mu - 1 ) % kk;
//
long long sum = 0 ;
for ( int i = 1 ; i <= n; i++ )
{
sum = ( sum + mul( i % mod, mu) ) % mod;
}
return sum;
}
} ;
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG1vZCwgbnA7CmxvbmcgbG9uZyBwWzMyMDEwXTsKCmNsYXNzIFBvd2VyaXQKewpwdWJsaWM6CiAgICBQb3dlcml0KCl7fQogICAgbG9uZyBsb25nIG11bChsb25nIGxvbmcgeCwgbG9uZyBsb25nIGspCiAgICB7CiAgICAgICAgaWYgKGsgPT0gMSkgcmV0dXJuIHg7CiAgICAgICAgbG9uZyBsb25nIGtrID0gbXVsKHgsIGsvMik7CiAgICAgICAgaWYgKGsgJSAyID09IDApCiAgICAgICAgICAgIHJldHVybiAoKGtrICoga2spICUgbW9kKTsKICAgICAgICBlbHNlCiAgICAgICAgICAgcmV0dXJuICgoKChrayAqIGtrKSAlIG1vZCkgKiB4KSAlIG1vZCk7CiAgICB9CgogICAgaW50IGNhbGMoaW50IG4sIGludCBrLCBpbnQgbSkKICAgIHsKICAgICAgICBtb2QgPSBtOwogICAgICAgIGludCBjcCA9IG07CiAgICAgICAgLy8gRUZUIHdpdGggbQogICAgICAgIGludCB4ID0gMjsKICAgICAgICB3aGlsZSAoeCAqIHggPD0gY3ApIHsKICAgICAgICAgICAgbG9uZyBsb25nIGtrID0gMTsKICAgICAgICAgICAgd2hpbGUgKG0gJSB4ID09IDApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGtrKj14OwogICAgICAgICAgICAgICAgbSA9IG0veDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoa2sgPiAxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBrayA9IChrayAvIHgpKih4IC0gMSk7CiAgICAgICAgICAgICAgICBucCsrOwogICAgICAgICAgICAgICAgcFtucF0gPSBrazsKICAgICAgICAgICAgfQogICAgICAgICAgICB4Kys7CiAgICAgICAgfQogICAgICAgIGlmIChtID4gMSkKICAgICAgICB7CiAgICAgICAgICAgIG5wKys7CiAgICAgICAgICAgIHBbbnBdID0gbTsKICAgICAgICB9CiAgICAgICAgbSA9IGNwOwogICAgICAgIGxvbmcgbG9uZyBrayA9IDE7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbnA7IGkrKykKICAgICAgICAgICAgIGtrICo9IHBbaV07CgogICAgICAgIC8vIDJeayAtIDEKICAgICAgICBsb25nIGxvbmcgbXUgPSAxOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IGs7IGkrKykKICAgICAgICAgICAgbXUgPSAobXUgKiAyKSAlIGtrOwogICAgICAgIG11ID0gKG11IC0gMSkgJSBrazsKICAgICAgICAvLwogICAgICAgIGxvbmcgbG9uZyBzdW0gPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIHN1bSA9IChzdW0gKyBtdWwoaSAlIG1vZCwgbXUpKSAlIG1vZDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN1bTsKICAgIH0KfTs=