#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;

#define     LL                      long long int
#define     LD                      long double
#define     ULL                     unsigned long long int

#define     pii                     pair<int, int>
#define     ump                     unordered_map
#define     mp                      make_pair
#define     mt                      make_tuple
#define     pb                      push_back
#define     ff                      first
#define     ss                      second
#define     SZ(x)                   (int)x.size()
#define     all(x)                  x.begin(), x.end()
#define     lb(x, y)                lower_bound(all(x), y)
#define     fill2d(arr, a, b, v)    fill(&arr[0][0], &arr[0][0] + a * b, v)
#define     fill3d(arr, a, b, c, v) fill(&arr[0][0], &arr[0][0] + a * b * c, v)
#define     rep(i, begin, end)      for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define     sp(x)                   setprecision(x) << fixed
#define     what_is(x)              cout << #x << " is " << x << endl
//#define     I64                     ll

#define     PI                      acos(-1.0)
#define     EPS                     1e-12
#define     mx                      200010
#define     S_SIZE                  2000010

const LL inf = 1000000000;
const LL mod = 1000000000 + 7;

inline void IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
inline int dcmp (LD x) { return x < -EPS ? -1 : (x > EPS); }

template<class T> inline int CHECK(T MASK, int i) { return (MASK >> i) & 1; }
template<class T> inline T ON(T MASK, int i) { return MASK | (T(1) << i); }
template<class T> inline T OFF(T MASK, int i) { return MASK & (~(T(1) << i)); }
template<typename T> inline int CNT(T MASK) {
    if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits) return __builtin_popcount(MASK);
    else return __builtin_popcountll(MASK);
}
template<class T> inline int RIGHT(T MASK) { return log2(MASK & -MASK); }

int dx4[] = { 0, 0, -1, +1 }; int dy4[] = { +1, -1, 0, 0 };
int dx8[] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 }; int dy8[] = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };

inline void I(int& a) { scanf("%d", &a); } inline void I(LL& a) { scanf("%I64d", &a); }
inline void I(ULL& a) { scanf("%I64u", &a); } inline void I(char* a) { scanf("%s", a); }
char Iarr[S_SIZE]; inline void I(string& a) { scanf("%s", Iarr); a = Iarr; }
template<typename T, typename... Args>
void I(T& a, Args&... args) { I(a); I(args...); }

inline void OUT(int a) { printf("%d", a); } inline void OUT(LL a) { printf("%I64d", a); }
inline void OUT(const char* a) { printf("%s", a); } inline void OUT(char* a) { printf("%s", a); }
inline void OUT(bool a) { printf("%d", a); } inline void OUT(string a) { rep(it, a.begin(), a.end()) printf("%c", *it); }
inline void OUT(ULL a) { printf("%I64u", a); }
template<typename T, typename... Args>
void OUT(T a, Args... args) { OUT(a); OUT(" "); OUT(args...); }
template<typename... Args>
void O(Args... args) { OUT(args...); OUT("\n"); }

template<typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> p) { os << "{" << p.first << ", " << p.second << "}"; return os; }
template<typename T>
inline ostream& operator<<(ostream& os, vector<T>& a) { os << "["; for (int i = 0; i < (int)a.size(); i++) { if (i) os << ", "; os << a[i]; } os << "]"; return os; }

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}

// mp.reserve(1024); mp.max_load_factor(0.25);
// head

const int M = 1000010; // maximum number of cubes

struct NumberTheory {
    LL gcd(LL a, LL b) { while (b) { a %= b; swap(a, b); } return a; }
    LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }
    LL bigMod(LL p, LL e, LL m) {
        LL ret = 1; p %= m;
        while (e > 0) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; e >>= 1; }
        return ret;
    }
    /// (1/a) % m when a & m are co-primes
    LL modInverse(LL a, LL m) {
        LL g = gcd(a, m);
        if (g != 1) {
            cout << "Inverse doesn't exist" << endl; return -1;
        }
        LL m0 = m;
        LL y = 0, x = 1;
        if (m == 1) return 0;
        while (a > 1) {
            LL q = a / m;
            LL t = m;
            m = a % m, a = t;
            t = y;
            y = x - q * y;
            x = t;
        }
        if (x < 0) x += m0;
        return x;
    }
    /// generate all (1/i) % m in [1..n] where n is less than m
    /// m must be prime
    void genModInv(int n, LL m, ULL inv[]) {
        inv[1] = 1;
        for (int i = 2; i <= n; ++i)
            inv[i] = (m - (m / i) * inv[m%i] % m) % m;
    }
    /// generate all (1/i!) % m in [1..n] where n is less than m
    /// m must be prime
    void genModInvFact(int n, LL m, ULL inv[], ULL invFact[]) {
        invFact[0] = invFact[1] = 1;
        for (int i = 2; i <= n; i++)
            invFact[i] = (invFact[i-1] * inv[i]) % m;
    }
} num;

struct CRT
{
    long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); }
    inline long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
    inline long long normalize(long long x, long long mod) { x %= mod; if (x < 0) x += mod; return x; }
    struct GCD_type { long long x, y, d; };
    GCD_type ex_GCD(long long a, long long b)
    {
        if (b == 0) return {1, 0, a};
        GCD_type pom = ex_GCD(b, a % b);
        return {pom.y, pom.x - a / b * pom.y, pom.d};
    }
    // Helper Functions

    pair<long long, long long> calc(int t, long long aa[], long long nn[])
    {
        // Make sure the arrays are 1 indexed
        long long* a = new long long[t+1];
        long long* n = new long long[t+1];
        long long ans, lcm;
        for(int i = 1; i <= t; i++) {
            a[i] = aa[i];
            n[i] = nn[i];
            normalize(a[i], n[i]);
        }
        ans = a[1];
        lcm = n[1];
        for(int i = 2; i <= t; i++) {
            auto pom = ex_GCD(lcm, n[i]);
            int x1 = pom.x;
            int d = pom.d;
            if((a[i] - ans) % d != 0) return cerr << "No solutions" << endl, mp(-1, -1);
            ans = normalize(ans + x1 * (a[i] - ans) / d % (n[i] / d) * lcm, lcm * n[i] / d);
            lcm = LCM(lcm, n[i]); // you can save time by replacing above lcm * n[i] /d by lcm = lcm * n[i] / d
        }
        return make_pair(ans, lcm);
    }
    // t is the number of congruences
    // Array is 1 indexed
    // x mod n1 = a1 mod n1
    // x mod n2 = a2 mod n2
    // x mod n3 = a3 mod n3
    // Here there is an unique value for a certain range.
    // This range will be n1 * n2 * n3 if all of them
    // are pairwise co-prime. But can be different otherwise.
    // The unique value is stored in "ans" and the range is
    // stored in "lcm".
    // The value repeats after "lcm".
    // If all the n's are not pairwise co-prime then there
    // might not be an answer.
} crt;

int main()
{
    LL a, b, p, x;
    I(a, b, p, x);
    LL res = 0;
    for (int i = 1; i <= p - 1; i++) {
        int t = 2;
        long long a1[3], n[3];
        n[1] = p - 1;
        n[2] = p;
        a1[1] = i;
        a1[2] = (b * num.modInverse(num.bigMod(a, i, p), p)) % p;
        pair<LL, LL> temp = crt.calc(t, a1, n);
        if (temp.ff <= x)
            res += (x - temp.ff) / temp.ss + 1;
    }
    O(res);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define ll long long
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
vector<int> rev,rt,one(1,1),zer;

#undef int

namespace Poly
{
	#define mod 31525197391593473LL
	#define int long long
	#define ull unsigned long long
	inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
	inline int add(int x,int y,int z){return add(add(x,y),z);}
	inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
	// inline ull mul(ull x,ull y){return (x*y-(ull)((long double)x/mod*y)*mod+mod)%mod;}
	inline int mul(int x,int y){return (__int128)x*y%mod;}
	inline int mul(int x,int y,int z){return mul(mul(x,y),z);}
	int qpow(int x,int y)
	{
		int ans=1;
		while(y)
		{
			if(y&1) ans=mul(ans,x);
			x=mul(x,x);
			y>>=1;
		}
		return ans;
	}
	int getInv(int x)
	{
		return qpow(x,mod-2);
	}
	int __inv[2000005];
	const int G=3;
	void Init_Inv()
	{
		__inv[0]=__inv[1]=1;
		for(int i=2;i<=2000000;i++) __inv[i]=mul(mod-mod/i,__inv[mod%i]);
	}
	void init(int B)
	{
		int n=1<<B;
		rev.resize(n),rt.resize(n);
		for(int i=1;i<n;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(B-1));
		for(int mid=1;mid<n;mid<<=1)
		{
			int w=qpow(G,(mod-1)/(mid<<1));
			rt[mid]=1;
			for(int i=1;i<mid;i++) rt[mid+i]=mul(rt[mid+i-1],w);
		}
	}
	void NTT(vector<int> &a,int B)
	{
		int n=1<<B;
		for(int i=0;i<n;i++) if(rev[i]>i) swap(a[i],a[rev[i]]);
		for(int mid=1;mid<n;mid<<=1)
		{
			for(int i=0;i<n;i+=(mid<<1))
			{
				for(int j=0;j<mid;j++)
				{
					int x=a[i+j],y=mul(a[i+j+mid],rt[j+mid]);
					a[i+j]=add(x,y),a[i+j+mid]=sub(x,y);
				}
			}
		}
	}
	vector<int> Mul(vector<int> a,vector<int> b)
	{
		int B=0,n=a.size()+b.size()-1;
		while((1<<B)<n) B++;
		a.resize(1<<B),b.resize(1<<B);
		init(B);
		NTT(a,B),NTT(b,B);
		for(int i=0;i<(1<<B);i++) a[i]=mul(a[i],b[i]);
		reverse(a.begin()+1,a.end());
		NTT(a,B);
		a.resize(n);
		int In=getInv(1<<B);
		for(int i=0;i<n;i++) a[i]=mul(a[i],In);
		return a;
	}
	#undef mod
};
#undef int

#define int long long
int n,m,mod;
inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int add(int x,int y,int z){return add(add(x,y),z);}
inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y){return 1LL*x*y%mod;}
inline int mul(int x,int y,int z){return mul(mul(x,y),z);}
vector<int> _QAQ;
vector<int> operator * (vector<int> x,vector<int> y)
{
	vector<ll> a(x.size()),b(y.size());
	for(int i=0;i<(int)x.size();i++) a[i]=x[i];
	for(int i=0;i<(int)y.size();i++) b[i]=y[i];
	vector<ll> r=Poly::Mul(a,b);
	vector<int> ans(r.size());
	for(int i=0;i<(int)r.size();i++) ans[i]=r[i]%mod;
	if((int)ans.size()>n+1) ans.resize(n+1);
	return ans;
}
vector<int> operator + (vector<int> a,vector<int> b)
{
	int n=max(a.size(),b.size());
	a.resize(n),b.resize(n);
	for(int i=0;i<n;i++) a[i]=add(a[i],b[i]);
	return a;
}
vector<int> operator - (vector<int> a,vector<int> b)
{
	int n=max(a.size(),b.size());
	a.resize(n),b.resize(n);
	for(int i=0;i<n;i++) a[i]=sub(a[i],b[i]);
	return a;
}
vector<int> a[200],b[200];
int cnt;
map<int,int> vis;
void print(vector<int> v)
{
	cout<<"-- ";
	for(int i :v) printf("%d ",i);
	cout<<"\n";
}
int solve(int x)
{
	if(vis[x]) return vis[x];
	vis[x]=++cnt;
	int cur=cnt;
	if(x==0)
	{
		a[cur]=b[cur]=zer;
		return cur;
	}
	int V=solve(x/2);
	vector<int> A=a[V],B=b[V];
	// print(A),print(B);
	if(x%2==0)
	{
		if((x/2)&1) a[cur]=A+(A+B+one)*B,b[cur]=B+(A+B+one)*A;
		else a[cur]=A+(A+B+one)*A,b[cur]=B+(A+B+one)*B;
	}
	else
	{
		int R=solve(x-1);
		vector<int> A=a[R],B=b[R];
		if(x&1) B=B+(A+B+one)*_QAQ;
		else A=A+(A+B+one)*_QAQ;
		a[cur]=A,b[cur]=B;
	/*
		vector<int> C=A,D=B;
		if((x/2)&1) C=C+(A+B+one)*_QAQ;
		else D=D+(A+B+one)*_QAQ;
		if((x/2)&1) a[cur]=A+(A+B+one)*C,b[cur]=A+(A+B+one)*D;
		else a[cur]=A+(A+B+one)*D,b[cur]=A+(A+B+one)*C;
	*/
	}
	return cur;
}
signed main()
{
	Poly::Init_Inv();
	_QAQ.resize(2); _QAQ[1]=1; zer.resize(1);
	// cout<<_QAQ[1]<<endl;
	cin>>n>>m>>mod;
	int ans=0;
	for(ll i=2;i<=m;i*=2)
	{
		int pl=solve(m/i);
		// printf("%d :\n",m/i);
		// for(int i=0;i<(int)a[pl].size();i++) printf("%d ",a[pl][i]); cout<<"\n";
		// for(int i=0;i<(int)b[pl].size();i++) printf("%d ",b[pl][i]); cout<<"\n";
		for(int i=1;i<(int)b[pl].size();i+=2) ans=add(ans,b[pl][i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N=100005;
int n,m;
int H(int x)
{
	if(x>=0)return 1;
	else return 0;
}
bool legal(int nx,int ny,int dx,int dy)
{
	if(nx==1 && ny==1 && dx==-1 && dy==-1)return false;
	if(nx==1 && ny==m && dx==-1 && dy==1)return false;
	if(nx==n && ny==1 && dx==1 && dy==-1)return false;
	if(nx==n && ny==m && dx==1 && dy==1)return false;
	return true;
}
map<int,int> vis[2][2][MAX_N];
map<int,int> used[MAX_N];
signed main()
{
	int nx,ny,dx,dy;
	cin>>n>>m;
	cin>>nx>>ny;
	string s;
	cin>>s;
	if(s[0]=='U')dx=-1;
	else dx=1;
	if(s[1]=='L')dy=-1;
	else dy=1;
	int ans=1;
	int cnt=0;
	while(true)
	{
	//	cout<<nx<<' '<<ny<<' '<<dx<<' '<<dy<<endl;
		if(!used[nx][ny])cnt++;
		used[nx][ny]=true;
		if(cnt==n+m-2)
		{
			cout<<ans<<endl;
			return 0;
		}
		if(nx==1)dx=1;
		if(ny==1)dy=1;
		if(nx==n)dx=-1;
		if(ny==m)dy=-1;
		if(vis[H(dx)][H(dy)][nx][ny])
		{
			cout<<-1<<endl;
			return 0;
		}
		vis[H(dx)][H(dy)][nx][ny]=true;
	/*	if(!legal(nx,ny,dx,dy))
		{
			cout<<-1<<endl;
			return 0;
		}*/
		if(dx==1 && dy==1)
		{
			int d1=n-nx;
			int d2=m-ny;
			int d=min(d1,d2);
			nx+=d;
			ny+=d;
			ans+=d;
		}
		if(dx==1 && dy==-1)
		{
			int d1=n-nx;
			int d2=ny-1;
			int d=min(d1,d2);
			nx+=d;
			ny-=d;
			ans+=d;
		}
		if(dx==-1 && dy==1)
		{
			int d1=nx-1;
			int d2=m-ny;
			int d=min(d1,d2);
			nx-=d;
			ny+=d;
			ans+=d;
		}
		if(dx==-1 && dy==-1)
		{
			int d1=nx-1;
			int d2=ny-1;
			int d=min(d1,d2);
			nx-=d;
			ny-=d;
			ans+=d;
		}
	}
	return 0;
}


#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline long long read()
{
	long long x=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
long long n,m,l,r;
long long mod;
vector<long long>p;
long long sum;
inline long long dfs(long long d,long long x,long long y)
{
	if(d==p.size())
	{
		long long n=abs(y/x);
		sum=(sum+1ll*n*(n+1)/2%mod*x%mod)%mod;
		return y/x;
	}
	return (dfs(d+1,x,y)+dfs(d+1,-x*p[d],y));
}
inline long long gcdnumber(long long a,long long y)
{
	p.clear();
	for(long long i=2;i*i<=a;i++){
		if(a%i==0){
			p.push_back(i);
			while(a%i==0) a/=i;
		}
	}
	sum=0;
	if(a!=1) p.push_back(a);
	return dfs(0,1,y);
}
inline long long calc(long long radiu,long long add)
{
	long long y=radiu;
	long long res=0;
	for(long long i=1;i<=radiu;i++)
	{
		while(y&&(long long)(i*i+y*y)>1ll*radiu*radiu-add) y--;
		if(y>=m) y=m;
		if(i<=n&&y>0)
		{
			long long tmp=gcdnumber(i,y);
			res=(res+1ll*(n-i+1)*(1ll*tmp*(m+1)%mod-sum+mod)%mod)%mod;
		}
	}
	return res;
}
int main()
{
	n=read(),m=read();l=read(),r=read(),mod=read();
	long long ans=0;
	if(l==1) ans=(1ll*n*(m+1)%mod+1ll*m*(n+1)%mod)%mod;
	ans=(ans+(calc(r,0)-calc(l,1)+mod)%mod*2%mod)%mod;
	cout<<ans<<endl;
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <queue>

using namespace std;

#define sqr(a)      ((a)*(a))
#define rep(i,a,b)  for(int i=(a);i<(int)(b);i++)
#define per(i,a,b)  for(int i=((a)-1);i>=(int)(b);i--)
#define PER(i,n)    per(i,n,0)
#define REP(i,n)    rep(i,0,n)
#define clr(a)      memset((a),0,sizeof (a))
#define SZ(a)       ((int)((a).size()))
#define ALL(x)      x.begin(),x.end()
#define mabs(a)     ((a)>0?(a):(-(a)))
#define inf         (0x7fffffff)
#define eps         1e-6

#define MAXN
#define MODN        (1000000007)

typedef long long ll;

int p[1000005];

int main()
{
    int a;
    scanf("%d",&a);

    clr(p);
    for (int i = 2;i < 1000005;i++)
    {
        if (p[i] == 0)
        {
            for (int j = i + i;j < 1000005;j += i)
            {
                p[j] = 1;
            }
        }
    }
    int temp = 0;
    for (int i = 10;i < 1000005;i++)
    {
        if (p[i] == 0)
        {
            int x = i;
            int y = 0;
            while (x)
            {
                y *= 10;
                y += x % 10;
                x /= 10;
            }
            if (p[y] == 0 && y != i)
            {
                temp++;
                if (temp == a)
                {
                    printf("%d\n",i);
                    break;
                }
            }
        }
    }

    return 0;
}


// code148C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>

using namespace std;
int main()
{
    long long n,m;
    scanf("%I64d %I64d",&n,&m);
    int p=1;
    int z=1000000009;
    for(int i=1;i<=m;i++)
    {
        p*=2;
        if(p>z) p%=z;
    }
    p+=z;
    long long ans=1;
    for(int i=1;i<=n;i++)
    {
        ans*=(p-i);
        ans%=z;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int mo=int(1e9)+7;

int quickpow(int x,int k) {
  int ret=1;
  while(k>0) {
    if(k&1) ret=1ll*ret*x%mo;
    x=1ll*x*x%mo;
    k>>=1;
  }
  return ret;
}

int n,m;
int main() {
  scanf("%d%d",&n,&m);
  printf("%I64d\n",1ll*quickpow(2*n+2,m)*(n+1-m)%mo*quickpow(n+1,mo-2)%mo);
  return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI1 3.14159265358979311599796346854418516159057617187500

typedef long long int ll;

int main()
{
    //FIO
    int t;
    //cin >> t;
    t=1;
    while(t--)
    {
        ll a,b,c,d;
        scanf("%lld %lld %lld %lld",&a,&b,&c,&d);

        ll rn1 = b-a+1;
        ll rn2 = c-b+1;
        ll rn3 = d-c+1;

        ll ans = rn1*rn2*rn3;
        ll pre[1000010]={0};
        for(int i=a;i<=b;i++)
        {
            pre[i+b]++;
            pre[i+c+1]--;
        }

        for(ll i=1;i<=d+1;i++)
            pre[i] += pre[i-1];

        for(ll i=1;i<=d;i++)
            pre[i] += pre[i-1];

        for(ll i=c;i<=d;i++)
            ans -= pre[i];

        printf("%lld",ans);

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x_floor,sum,diff_1,diff_2,diff_3,add=0;
    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    for(int i=1;i<=n;i++)
    {
        int total=0,mini=INT_MAX;
        for(int j=1;j<=n;j++)
        {
        diff_1=abs(j-i)*2;
        diff_2=abs(i-1)*2;
        diff_3=(j-1)*2;
        sum=diff_1+diff_2+diff_3;

        //cout<<sum*arr[i-1]<<" ";

        total=sum*arr[i-1];

        mini=min(mini,total);

        }
        add=add+mini;
    }

    cout<<add<<"\n";

    return 0;
}


#include<iostream>
using namespace std;
int n,m;
int a[17];
int main(){
	cin>>n>>m;
	while(n!=0){
		if(a[n%m]){
			cout<<"NO";
			return 0;
		}
		a[n%m]=1;
		n/=m;
	}
	cout<<"YES";
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const ll inv2 = 499122177;
int main(){
	int n;scanf("%d",&n);
	ll dp1=inv2,dp2=inv2*inv2%mod;
	if (n==1){
		printf("%lld\n",dp1);
		return 0;
	}
	for (int i=3;i<=n;++i){
		dp1 = inv2*((inv2*dp1%mod+dp2)%mod)%mod;
		swap(dp1,dp2);
	}printf("%lld\n",dp2);
}


#include<bits/stdc++.h>
#define ll long long
#define mkp make_pair
#define inf 0x3f3f3f3f
using namespace std;
const int mod=1e9+7;
int a[100005];
ll qpow(ll a,ll b)
{
	ll sum=1;
	while(b)
	{
		if(b&1)sum=(sum*a)%mod;
		a=a*a%mod;
		b>>=1;
	}
	return sum;
}
int main()
{
	ll n,m,k,r,c;
	scanf("%lld%lld%lld%lld%lld",&n,&m,&k,&r,&c);
	ll ax,ay,bx,by;
	scanf("%lld%lld%lld%lld",&ax,&ay,&bx,&by);
	ll sum=1;
	if(ax>bx)
	{
		swap(ax,bx);
		swap(ay,by);
	}
	if(ay>by)
	{
		ay=m-ay-c;
		by=m-by-c;
	}
	//printf("%lld %lld %lld %lld\n",ax,bx,ay,by);
	if(ax==bx&&ay==by)
	{
		printf("%lld\n",qpow(k,n*m));
	}
	else
	{
		printf("%lld\n",qpow(k,n*m-r*c));
	}
 }


#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
const long long MD = 998244353;
template<long long MOD=MD> struct mdint {
    int d=0;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
int main() {
    int n; cin >> n;
    n++;
    vector<mint> dp(n+1),dp2(n+1);
    // dp[n] = number of non isomorphic binary trees with depth <= n
    // dp2[n] = number of non isomorphic binary trees with max depth = n.
    // depth = max number of nodes of any root to leaf path in the binary tree.
    dp[0]=1; // empty tree
    dp2[0]=1; // empty tree
    const mint halve = mint(1)/2;
    for(int i=1;i<=n;++i) {
        dp[i] = 1; // empty tree
        dp[i] += (dp[i-1]*(dp[i-1]+1))*halve; // all combinations of <=i-1 depth trees hanging underneath!
        dp2[i] = dp[i]-dp[i-1];
    }
    // if three trees hanging off root.
    mint one=dp2[n-1]*dp[n-2]*(dp[n-2]+1)/2; // one subtree has maximal depth
    mint two=dp2[n-1]*(dp2[n-1]+1)*halve*dp[n-2]; // two subtrees have depth of n-1
    mint three = dp2[n-1]*(dp2[n-1]+1)*(dp2[n-1]+2)/6; // all 3 subtrees have depth of n-1

    mint ans = (one+two+three)*2 -1;
    // need all trees which don't all connect to root.
    for(int i=1;i<n;++i) {
        // don't count the degenerate tree which is a path on the left side
        ans+=(dp2[i]-1)*(dp2[n-i]-dp2[n-i-1]);
        // make sure the right side is a degree 3 vertex, so no double counting.
    }
    cout << ans << '\n';
}


#include<bits/stdc++.h>
#define N 20
using namespace std;
int a[N],b[N];
int main(){
    double n,egg=0;
    scanf("%lf",&n);
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int i=0;i<n;i++)scanf("%d",&b[i]);
    for(int i=0;i<1e7;i++)
        for(int j=0;j<n;j++)
            if(b[j]==i%a[j]){
                egg++;
                break;
            }
    //1234
    printf("%.6lf\n",egg/1e7);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 20;

const int c0[] = { 2, 1, 0, 1, 2 };
const int c1[] = { 6, 7, 8, 7, 6 };
const int di[] = { 1, 1, 0, 0, -1, -1 };
const int dj[] = { -1, 1, 2, -2, -1, 1 };

int dp[1 << MAXN];
char ts[16], s[16][16], a[16][16];

int gao() {
	int mask = 0, po = 0;
	for (int i = 0; i < 5; ++i) {
		for (int j = c0[i]; j <= c1[i]; j += 2) {
			if (s[i][j] == 'O') {
				mask |= (1 << po);
			}
			++po;
		}
	}
	int &ret = dp[mask];
	if (ret == -1) {
		ret = 0;
		for (int i = 0; i < 5 && !ret; ++i) {
			for (int j = c0[i]; j <= c1[i] && !ret; j += 2) {
				if (s[i][j] != 'O')
					continue;
				for (int k = 0; k < 6 && !ret; ++k) {
					int ii = i, jj = j;
					while (ii >= 0 && ii < 5 && jj >= c0[ii] && jj <= c1[ii] && s[ii][jj] == 'O') {
						s[ii][jj] = 'X';
						ii += di[k];
						jj += dj[k];
						if (!gao()) {
							ret = 1;
							break;
						}
					}
					while (ii != i || jj != j) {
						ii -= di[k];
						jj -= dj[k];
						s[ii][jj] = 'O';
					}
				}
			}
		}
	}
	return ret > 0;
}

int main() {
	memset(dp, -1, sizeof(dp));
	for (int i = 0; i < 16; ++i)
		for (int j = 0; j < 16; ++j)
			s[i][j] = '?';
	for (int i = 0; i < 5; ++i) {
		for (int j = c0[i]; j <= c1[i]; j += 2) {
			scanf("%s", ts);
			s[i][j] = ts[0];
		}
	}
	printf("%s\n", gao() ? "Karlsson" : "Lillebror");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    long long int n;
    cin>>n;
    int a=log2(n);
    cout<<(a+1)<<endl;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const int N = 105;
int a[3], dp[N][N][N];
string s[3];
vector<int> f;
vector<int> computePrefix(string pat)
{
    int m = pat.size();
    vector<int> lp(m);
    for(int i = 1, k = 0; i < m; i++)
    {
        while(k > 0 && pat[k] != pat[i])
            k = lp[k - 1];
        if(pat[k] == pat[i])
            lp[i] = ++k;
        else
            lp[i] = k;
    }
    return lp;
}
int solve(int i, int j, int k)
{
    if(k == a[2])
        return -1e9;
    if(i == a[0] || j == a[1])
        return 0;
    int &x = dp[i][j][k];
    if(x != -1)
        return x;
    int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9;
    if(s[0][i] == s[1][j])
    {
        int l = k;
        while(l > 0 && s[2][l] != s[0][i])
            l = f[l - 1];
        if(s[2][l] == s[0][i])
            ++l;
        z3 = 1 + solve(i + 1, j + 1 , l);
    }
    return x = max(z1, max(z2, z3));
}
void print(int i, int j, int k)
{
    if(k == a[2])
        return;
    if(i == a[0] || j == a[1])
        return;
    int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9, x = -1e9, l = k;
    if(s[0][i] == s[1][j])
    {
        while(l > 0 && s[2][l] != s[0][i])
            l = f[l - 1];
        if(s[2][l] == s[0][i])
            ++l;
        z3 = 1 + solve(i + 1, j + 1 , l);
    }
    x = max(z1, max(z2, z3));
    if(x == z1)
        return print(i + 1, j, k);
    else if(x == z2)
        return print(i, j + 1, k);
    else
    {
        cout << s[0][i];
        print(i + 1, j + 1, l);
    }
}
int main()
{
    IO;
    for(int i = 0; i < 3; i++)
        cin >> s[i],
        a[i] = s[i].size();
    f = computePrefix(s[2]);
    memset(dp, -1, sizeof dp);
    if(!solve(0, 0, 0))
        return cout << 0, 0;
    print(0, 0, 0);
}


#include <bits/stdc++.h>
using namespace std;
const int ZERO = 151;
const int MAXN = 305;
int maxdep;
int dr[] = {1,1,1,0,0,-1,-1,-1};
int dc[] = {-1,0,1,-1,1,-1,0,1};
int split_dir[][2] = {{1,3},{0,2},{1,4},{0,5},{2,7},{3,6},{5,7},{6,4}};
bool split[MAXN], board[MAXN][MAXN], vis[MAXN][MAXN][MAXN][8];
void recurse(int depth, int r, int c, int dir)
{
	if(depth > maxdep)
		return;
	board[r][c] = true;
	if(vis[r][c][depth][dir])
		return;
	vis[r][c][depth][dir] = true;
	if(split[depth])
	{
		int d1 = split_dir[dir][0], d2 = split_dir[dir][1];
		int r1 = r + dr[d1], c1 = c + dc[d1];
		int r2 = r + dr[d2], c2 = c + dc[d2];
		recurse(depth+1,r1,c1,d1);
		recurse(depth+1,r2,c2,d2);
	}
	else
	{
		recurse(depth+1,r + dr[dir],c + dc[dir],dir);
	}
}
int main()
{
	int n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		int x;
		cin>>x;
		maxdep+=x;
		split[maxdep] = true;
	}
	recurse(1,ZERO,ZERO,1);
	int ans = 0;
	for (int i = 0; i < MAXN; ++i)
	{
		for (int j = 0; j < MAXN; ++j)
		{
			if(board[i][j])
				ans++;
		}
	}
	cout<<ans<<"\n";
	return 0;
}


//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;

int n, m;

int s;
ll t;

ll bylem[nax];

void ruch()
{
	if (s<m)
	{
		s=(s+t)%n;
	}
	else
	{
		s=(s-t)%n;
		if (s<0)
			s+=n;
	}
	t--;
}

int main()
{
	scanf("%d%d", &n, &m);
	scanf("%d%lld", &s, &t);
	s--;
	while(1)
	{
		if (bylem[s])
			t%=(bylem[s]-t);
		bylem[s]=t;
		if (t<=1e6)
		{
			while(t)
				ruch();
			printf("%d\n", s+1);
			return 0;
		}
		for (int i=0; i<n; i++)
			ruch();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,b,e) for(int i=b;i<=e;i++)
#define loop2(i,e,b) for(int i=e;i>=b;i--)
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define X real()
#define Y imag()
#define cp(a,b)	( (conj(a)*(b)).imag() )
#define length(a) (hypot((a).imag(), (a).real()))
typedef complex<double> point;
typedef long long ll;
typedef unsigned long long ull;
double PI = 3.141592653589793;
const double EPS = 1e-9;
const int N = 1e6 + 5;
const ll mod = 1e9 + 7;
const int oo = 1e9;
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

ll n;
vector<ll> v;

int main() {
	scanf("%lld", &n);
	for (ll i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			v.push_back(i);
			if (i * i != i) v.push_back(n / i);
		}
	}
	ll mx = -1, mn = 1e18;
	for (int i = 0; i < v.size(); i++) {
		for (int j = 0; j < v.size(); j++) {
			ll c = n / (v[i] * v[j]);
			if (n % (v[i] * v[j]) != 0) continue;
			ll x = (v[i] + 1) * (v[j] + 2) * (c + 2);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
			x = (v[i] + 2) * (v[j] + 1) * (c + 2);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
			x = (v[i] + 2) * (v[j] + 2) * (c + 1);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
		}
	}
	printf("%lld %lld", mn, mx);
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int P = 1e9 + 7;
int phi[N];

void initPhi() {
  for (int i = 2;i < N;i++) {
    if (!phi[i]) {
      for (int k = i;k < N;k += i) {
        if (!phi[k]) phi[k] = k;
        phi[k] = phi[k] / i * (i - 1);
      }
    }
  }
}
vector<int> factor[N];
void initFactor() {
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      factor[j].push_back(i);
    }
  }
}

int gcd(int a, int b) {
  return b ? gcd(b, a % b) : a;
}

void solve() {
  initPhi();
  initFactor();

  int n;
  cin >> n;
  int64_t ans = 0;
  for (int c = 1; c < n - 1; c++) {
    for (int d : factor[n - c]) {
      ans += 1LL * c * d / gcd(c, d) * phi[(n - c) / d] % P;
    }
  }
  cout << ans % P << endl;
}

int main(){
  solve();
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 70;
const int MOD = 1000000007;
LL n , t , dp[N][N];
int p = 0;
LL dfs (int l , int c , int limit , LL n) {
    if (l < 0) return c == p;
    if (!limit && dp[l][c] != -1) return dp[l][c];
    int up = limit ? ((n >> l) & 1) : 1;
    LL ret = 0;
    for (int i = 0 ; i <= up ; i ++) {
        ret += dfs (l - 1 , c + i , limit && i == up , n);
    }
    if (!limit) dp[l][c] = ret;
    return ret;
}
LL gao (LL n) {
    int t = 0;
    while ((1LL << t) < n) t ++;
    return dfs (t , 0 , 1 , n);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen ("input.txt" , "r" , stdin);
    // freopen ("output.txt" , "w" , stdout);
#endif
    cin >> n >> t;
    if ((t & (-t)) != t) {
        puts ("0");
        return 0;
    }
    while ((1LL << p) < t) p ++;
    p ++;
    memset (dp , -1 , sizeof (dp));
    cout << gao (n + 1) - gao (1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = a; i < n; ++i)
#define ford(i, a, n) for (int i = n - 1; i >= a; --i)
#define fore(i, a, n) for (int i = a; i <= n; ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)\
    for (auto i : a) cerr << i << ' ';\
    cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

const int N = 2017;
ll dp[N][N];
ll sum[N][N];

ll Add(ll x, ll y) {
    x += y;
    if (x >= MOD) x -= MOD;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    forn(i, 0, N) dp[0][i] = 0;
    forn(i, 0, N) sum[0][i] = 0;
    forn(i, 1, N) {
        dp[i][2] = Add(sum[i - 1][2], 1);
        forn(j, 3, N)
            dp[i][j] = Add(dp[i][j - 1], sum[i - 1][j]);
        sum[i][0] = dp[i][0];
        forn(j, 1, N)
            sum[i][j] = Add(sum[i][j - 1], dp[i][j]);
    }
    int n, m;
    cin >> n >> m;
    ll ans = 0;
    forn(i, 0, n)
        fore(j, 2, m) {
            ans += (m - j + 1) * dp[i + 1][j] % MOD * (dp[n - i][j] - dp[n - i - 1][j] + MOD);
            ans %= MOD;
        }
    cout << ans << '\n';
}


#pragma comment(linker, "/STACK:167177216")

#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<iomanip>

using namespace std;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

int t2(int a, int b)
{
    int k = 1;
    forn (i, 100)
    {
        k *= a;
        if(k % b == 0)
            return i + 1;
        k %= b;
    }
    return 0;
}

bool t3(int a, int b)
{
    if((a % b) == 1)
        return true;
    return false;
}

bool t11(int a, int b)
{
    if(a % b == 1 && a * a % b == b - 1)
        return true;
    if(a % b == b - 1 && a * a % b == 1)
        return true;
    return false;
}

bool t6(int a, int b)
{
    int a2 = 0, a3 = 0, a11 = 0;
    for (int i = 2; i * i <= b; ++i)
    {
        if(b % i == 0)
        {
            int perm = 1;
            while(b % i == 0)
            {
                perm *= i;
                b /= i;
            }
            bool ok = false;
            if(t2(a, perm))
                ok = a2 = 1;
            if(t3(a, perm))
                ok = a3 = 1;
            if(t11(a, perm))
                ok = a11 = 1;
            if(!ok)
                return false;
        }
    }
    if(b > 1)
    {
        bool ok = false;
        int perm = b;
        if(t2(a, perm))
            ok = a2 = 1;
        if(t3(a, perm))
            ok = a3 = 1;
        if(t11(a, perm))
            ok = a11 = 1;
        if(!ok)
            return false;
    }
    return a2 + a3 + a11 >= 2;
}

int main()
{

    int a, b;
    cin >> a >> b;

    if(t2(a, b))
        cout << "2-type" << endl << t2(a, b) << endl;
    else
        if(t3(a, b))
            cout << "3-type" << endl;
        else
            if(t11(a, b))
                cout << "11-type" << endl;
            else
                if(t6(a, b))
                    cout << "6-type" << endl;
                else
                    cout << "7-type" << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll qpow( ll x , ll y)
{
    ll ans = 1;
    x %= mod;
    while( y ){
        if( y & 1 )
            ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}
typedef vector<int> VI;
namespace linear_seq{
    const int n = 10010;
    ll res[n], base[n], c[n], md[n];
    vector < int > Md;
    void mul( ll *a, ll *b, int k){
        for ( int i = 0; i < k + k; i++){
            c[i] = 0;
        }
        for ( int i = 0; i < k; i++){
            if ( a[i] ){
                for (int j = 0; j < k; j++){
                    c[ i + j ]=( c[ i + j ] + a[i] * b[j] ) % mod;
                }
            }
        }
        for (int i = k + k - 1; i >= k; i--){
            if ( c[i] ){
                for (int j = 0; j < (int)Md.size(); j++){
                    c[ i - k + Md[j] ] = ( c[ i - k + Md[j] ] - c[i] * md[ Md[j] ]) % mod;
                }
            }
        }
        for (int i = 0; i < k; i++){
            a[i] = c[i];
        }
    }

    ll solve( ll n, VI a, VI b){
        ll ans = 0, pnt = 0;
        int k = (int)a.size();
        assert( (int)a.size() == (int)b.size() );
        for (int i = 0; i < k; i++){
            md[ k - 1 - i] = - a[i];
        }
        md[k] = 1;
        Md.clear();
        for ( int i = 0; i < k; i++){
            if ( md[i] != 0){
                Md.push_back(i);
            }
        }
        for ( int i = 0; i < k; i++){
            res[i] = base[i] = 0;
        }
        res[0] = 1;
        while ( ( 1ll << pnt ) <= n ){
            pnt++;
        }
        for (int p = pnt; p >= 0; p--){
            mul( res, res, k);
            if ( ( n>>p ) & 1){
                for (int i = k - 1; i >= 0; i--){
                    res[ i + 1 ]=res[i];
                }
                res[0] = 0;
                for (int j = 0; j< (int) Md.size(); j++){
                    res[ Md[j] ]=( res[ Md[j] ]- res[k] * md[ Md[j] ] ) % mod;
                }
            }
        }
        for (int i = 0; i < k; i++){
            ans=( ( ans + res[i] * b[i] ) % mod + mod ) % mod;
        }
        return ans;
    }
    VI BM( VI s){
        VI C( 1 , 1 ), B( 1 , 1 );
        int L = 0, m = 1, b = 1;
        for ( int n = 0; n < (int)s.size(); n++){
            ll d = 0;
            for (int i = 0; i < L + 1; i++){
                d=( d + (ll)C[i] * s[ n - i ] ) % mod;
            }
            if ( d == 0 ){
                m++;
            }
            else if ( 2 * L <= n ){
                VI T = C;
                ll c = mod - d*qpow( b, mod - 2 ) % mod;
                while ( (int)C.size() < (int)B.size() + m){
                    C.push_back(0);
                }
                for ( int i = 0; i < (int)B.size(); i++){
                    C[ i + m ] = ( C[ i + m ] + c * B[ i ] ) % mod;
                }
                L = n + 1 - L;
                B = T;
                b = d;
                m = 1;
            }
            else {
                ll c = mod - d * qpow( b , mod - 2 ) % mod;
                while ( (int)C.size() < (int)B.size() + m ){
                    C.push_back(0);
                }
                for (int i = 0; i < (int)B.size(); i++){
                    C[ i + m ]= ( C[ i + m ] + c * B[ i ] ) % mod;
                }
                m++;
            }
        }
        return C;
    }
    ll gao( VI a, ll n){
        VI c = BM( a );
        c.erase( c.begin() );
        for (int i = 0; i < (int)c.size(); i++){
            c[i]= ( mod - c[i] )%mod;
        }
        return solve( n, c, VI( a.begin(), a.begin() + (int)c.size() ) );
    }
};
ll f[300];
int main()
{
    int m;
    ll n;
    scanf("%lld%d",&n,&m);
    VI a;
    for(int i=1;i<=m;i++){
        f[i]=1;
    }
    for(int i=m;i<=200;i++){
        f[i]=(f[i-1]+f[i-m])%mod;
    }
    for(int i=1;i<=200;i++){
        a.push_back(f[i]);
    }
    n++;
    ll ans=linear_seq::gao(a,n-1);
    printf("%lld\n",ans%mod);
    return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main()
{
    int  t;

    cin>>t;

    int n=t;

    int p1=1;
    int p2=2;
    int s=3;

   int a[t];

   for(int i=0;i<t;i++) {
   	cin>>a[i];
   }

   if(a[0]==3) {
   	cout<<"NO";
   	return 0;
   }

  for(int i=0;i<t;i++) {
  	if(a[i]!=p1&&a[i]!=p2) {
  		cout<<"NO";
  		return 0;
  	}

    else {
    	p1=a[i];
    	p2=s;
    	s=6-a[i]-s;
    }

  }

  cout<<"YES";

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef int INT;
#define int long long
#define x first
#define y second
typedef pair<int,int> PR;
vector<int> a[20];
#define MK make_pair
#define PB push_back
const int Mx=(1<<16)-1;
int mod=10;
int L=60;
int n;
int mul(int x,int y){
	int ret=0;
	for(int i=0;i<3;i++){
		ret+=(x&Mx)*y%mod;
		x>>=16;
		y=(y<<16)%mod;
	}
	return ret%mod;
}
PR Calc(int x){
	if(x==1)return MK(0,1);
	PR A=Calc(x&1?x-1:x/2);
//	cout<<A.x<<" "<<A.y;
//	puts("fuck");
	if(x&1)return MK(A.y,(A.x+A.y)%mod);
//	puts("fuckstep2");
	return MK((mul(A.x,A.x)+mul(A.y,A.y))%mod,mul(A.x*2+A.y,A.y)%mod);
}
INT main(){
// 	freopen("a.in","r",stdin);
	cin>>n;
	for(int i=1;i<=60;i++)if(Calc(i).x==n%mod) a[0].PB(i);
	for(int t=0;t<12;t++){
		if(a[t].size()==0)break;
		mod=mod*10;
		for(int k=0;k<a[t].size();k++){
			int i=a[t][k];
			for(int j=0;j<=9;j++){
				if(Calc(L*j+i).x==n%mod)a[t+1].PB(L*j+i);
			}
		}
		L=L*10;
	}
	int ans=-1;
	if(a[12].size()==0){
		cout<<ans;
		return 0;
	}
	for(int k=0;k<a[12].size();k++){
		int i=a[12][k];
		if(ans<0)ans=i-1;
		if(i-1<ans)ans=i-1;
	}
	cout<<ans;
	return 0;
}


/*
ID: espr1t
LANG: C++
TASK: Demo
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <set>

#define MAX 16

using namespace std;
FILE *in; FILE *out;

struct Edge
{
        int lo, hi, prc;
};

int n, all, flow, cur;
int cap[MAX];
Edge edges[MAX][MAX];

void recurse(int node, int next, int price)
{
        if (node == n - 1)
        {
                if (cap[node] == flow)
                        cur = max(cur, price);
                return;
        }

        if (next >= n)
        {
                if (cap[node] == 0)
                        recurse(node + 1, node + 2, price);
        }
        else
        {
                for (int i = edges[node][next].lo; i <= edges[node][next].hi; i++)
                {
                        if (cap[node] < i) break;
                        cap[node] -= i; cap[next] += i;
                        int nprice = price + i * i;
                        if (i > 0) nprice += edges[node][next].prc;
                        recurse(node, next + 1, nprice);
                        cap[node] += i; cap[next] -= i;
                }
        }
}

int eval()
{
        for (int i = 0; i < n; i++)
                cap[i] = 0;
        cap[0] = flow;
        cur = -1;
        recurse(0, 1, 0);
        return cur;
}

int main(void)
{
        in = stdin; out = stdout;
//      in = fopen("C.in", "rt"); out = fopen("C.out", "wt");

        fscanf(in, "%d", &n);
        all = (n * (n - 1)) / 2;
        for (int i = 0; i < all; i++)
        {
                int n1, n2;
                fscanf(in, "%d %d", &n1, &n2);
                n1--; n2--;
                fscanf(in, "%d", &edges[n1][n2].lo);
                fscanf(in, "%d", &edges[n1][n2].hi);
                fscanf(in, "%d", &edges[n1][n2].prc);
        }

        int ans = -1, best = -1;
        for (flow = 0; flow <= 25; flow++)
        {
                best = eval();
//              cout << "Eval with flow = " << flow << " gives " << best << endl;
//              system("pause");
                if (best != -1) {ans = flow; break;}
        }
        fprintf(out, "%d %d\n", ans, best);

        return 0;
}


#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int xp , yp , xv , yv;
int cal(int x){
	return (x >= 0 ? x : -x);
}
main(){
//	freopen("test.inp","r",stdin);
	scanf("%d %d %d %d",&xp,&yp,&xv,&yv);
	if(xv + yv < xp + yp) printf("Vasiliy");
	else{
		int dia1 = xv - yv;
		int dia2 = xp - yp;
		bool can_reach = true;
		int step = cal(dia1 - dia2);
		if(dia1 > dia2 && yp < step) can_reach = false;
		if(dia2 > dia1 && xp < step) can_reach = false;
		if(!can_reach) printf("%s", max(xv , yv) < xp + yp ? "Vasiliy" : "Polycarp");
		else{
			int l1 = xp + yp - step;
			int l2 = xv + yv - (step <= min(xv , yv) ? 2*step : step + min(xv , yv));
			printf("%s",(l1 <= l2) ? "Polycarp" : "Vasiliy");
		}
	}

}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
LL gao(LL l1,LL r1,LL l2,LL r2)
{
    if(l1==l2&&r1==r2)
        return 0;
    if(r1<=l2)
        return l2-r1+1;
    else
        return l1-r2+1;
}
long long L(LL x,LL m)
{
    if(x>0)
    {
        LL y=x%m;
        return (x-y)/m;
    }
    else
    {
        x=-x;
        LL y=x%m;
        return -(x+(m-y))/m;
    }
}
long long R(long long x,LL m)
{
    if(x>0)
    {
        LL y=x%m;
        return (x+m-y)/m;
    }
    else
    {
        x=-x;
        LL y=x%m;
        return -(x-y)/m;
    }
}
int main()
{
    long long a,b,x1,y1,x2,y2;
    cin>>a>>b>>x1>>y1>>x2>>y2;
    a*=2,b*=2;
    LL u1=R(x1+y1,a);
    LL d1=L(x1+y1,a);
    LL u2=R(x2+y2,a);
    LL d2=L(x2+y2,a);
    LL ans=gao(d1,u1,d2,u2);
    u1=R(y1-x1,b);
    d1=L(y1-x1,b);
    u2=R(y2-x2,b);
    d2=L(y2-x2,b);
    ans=max(ans,gao(d1,u1,d2,u2));
    cout<<ans;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>
#include <queue>
#include <utility>
#include <set>
#include <stack>
#include <vector>
#include <map>
#define YOU using
#define DONT namespace
#define SAY std
YOU DONT SAY;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
const int OO = 1e9;
const ll INF = 1e18;
const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}
const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}
#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;
	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}
inline void ini(int& x){
	x = getnum<int>();
}
inline void scani(int& x){
	scanf("%d",&x);
}
int n,m;
int solve(int m, int n){
	if(n == 1)return -1;
    else if(n == 2 && m > 3)return -1;
    else if(n == 3 && m > 5)return -1;
    else if(n == 2 && m == 2)return 5;
    else if(m == 3 && n == 2)return 11;
    else if(n >= 2*m)return 1;
    else if(n > m && n < 2*m)return 3;
    else if(n == 3){
    	if(m == 3)return 5;
    	else if(m == 4)return 9;
    	else if(m == 5)return 11;
	}else{
		int n7 = (n-1)/2;
		int nl = (n-2)/2;

		m -= n;
		m++;
		int curr = 5;
		while(1){
			if(curr == 7)m -= n7;
			else m -= nl;
			if(m <= 0)return curr;
			curr += 2;
		}
	}
}
int main(){
	cin>>m>>n;
    cout << solve(m, n) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
const int maxn = 111;
int n;
int p[maxn] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
ull ret;

void dfs(int depth, ull cur, int cnt, int hi) {
  if(depth >= 10 || cnt > n) return;
  if(cnt == n) {
    ret = min(ret, cur);
    return;
  }
  for(int i = 1; i <= hi; i++) {
    if(cnt * (i + 1) > n) break;
    cur *= (ull)p[depth];
    dfs(depth+1, cur, cnt*(i+1), i);
  }
}

signed main() {
  // freopen("in", "r", stdin);
  while(~scanf("%d",&n)) {
    ret = 1E18;
    dfs(1, 1, 1, 60);
    cout << ret << endl;
  }
  return 0;
}


//#pragma comment(linker, "/STACK:134217728")

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
using namespace std;

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair

Int A[1 << 20];

Int f(Int n)
{
	if(n % 3 != 0)
		return 0;

	int i, j;
	n /= 3;
	Int res = 0;
	Int c;
	int cnt = 0;
	for(c = 2; c*c < n; ++c)
		if(n % c == 0)
		{
			A[cnt] = c;
			++cnt;
		}

	FOR(i, 0, cnt)
		FOR(j, 0, i + 1)
		{
			Int r = 0;
			Int ab = A[i];
			Int ac = A[j];
			Int bc = ab - ac;

			Int t = (ab + ac)*(ab + ac) - bc*bc;
			if((4*n) % t == 0)
			{
				bc = 4*n/t;
				Int d = (ab + bc + ac);
				if(d % 2 != 0)
					continue;

				d /= 2;
				Int a = d - bc;
				Int b = d - ac;
				Int c = d - ab;
				if(a <= 0 || b <= 0 || c <= 0)
					continue;

				if(d*d*d - a*a*a - b*b*b - c*c*c == 3*n)
					++r;
			}

			res += r;
			if(i != j)
				res += r;
		}

	//for(c = 2; c*c <= n; ++c)
	//	if(n % c == 0)
	//		for(a = 1; a < c; ++a)
	//		{
	//			b = c - a;
	//			Int A = 1;
	//			Int B = a + b;
	//			Int C = a*b - n/(a + b);
	//			Int D = B*B - 4*A*C;
	//			if(D < 0)
	//				continue;

	//			Int Min = B, Max = 1 << 30;
	//			while(Max - Min > 1)
	//			{
	//				Int Mid = (Max + Min)/2;
	//				if(Mid*Mid > D)
	//					Max = Mid;
	//				else
	//					Min = Mid;
	//			}

	//			if(Min*Min == D)
	//				++res;
	//		}

	return res;
}

int main()
{
	Int n;
	scanf("%lld", &n);

	printf("%lld\n", f(n));
	return 0;
};


#include <cstdio>
#include <algorithm>
using namespace std ;
int solver( int l1 , int r1 , int l2 , int r2 , int step = 30 ){
       if( l1 > r1 || l2 > r2 ) return 0 ;  //结束条件
       int ovl = max( l1 , l2 ) ;
       int ovr = min( r1 , r2 ) ;
       int overlap = ( (ovl <= ovr) ? (ovr - ovl + 1) : 0 ) ; //算出重叠部分
       if( l1 <= l2 && r1 >= r2 || l1 >= l2 && r1 <= r2 ) return overlap ;  //第一种情况，包含的情况，直接返回重叠部分
       int m = ( 1 << ( step - 1 ) ) ;//取中间值
       int res = overlap ;
       res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;
       res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;
       res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;
       res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;
       return res ;
}

int main()
{
       int l1 , l2 , r1 , r2 ;
       while( ~scanf("%d%d%d%d" , &l1 , &r1 , &l2 , &r2 ) )
              printf("%d\n" , solver( l1 , r1 , l2 , r2 ) ) ;
       return 0 ;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double fmod(double a, double b){
    return a - b * (int)(a / b);
}
double nd(){
    double n;
    scanf("%lf", &n);
    return n;
}

double a , b, m, vx,vy,vz;

double findX(double xx){
    if (xx >= 0){
        double rmd = fmod(xx, a);
        int f1 = (int)(xx / a);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return a - rmd;
        }
    }
    else {
        xx *= -1;
        double rmd = fmod(xx, a);
        int f1 = (int)(xx / a);
        if (f1 % 2 == 0){
            return rmd;

        }
        else {
            return a - rmd;
        }
    }
}
double findY(double yy){
    if (yy >= 0){
        double rmd = fmod(yy, b);
        int f1 = (int)(yy / b);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return b - rmd;
        }
    }
    else {
        yy *= -1;
        double rmd = fmod(yy, b);
        int f1 = (int)(yy / b);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return b - rmd;

        }
    }
}
int main(){
    a = nd();
    b = nd();
    m = nd();
    vx = nd();
    vy = nd();
    vz = nd();

    double times = m / -vy;
    double xx = a / 2.0;
    double zz = 0;
    xx += times * vx;
    zz += times * vz;
    // printf("%f %f\n", xx, zz);
    xx = findX(xx);
    zz = findY(zz);
    printf("%f %f\n", xx, zz);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int n,x,a[1000000],tot;
long long modpow(long long x,long long y,int p){
	long long res=1;
	for(;y;y>>=1,(x*=x)%=p)
		if(y&1)(res*=x)%=p;
	return res;
}
bool check(int b,int p){
	for(int i=1;i*i<p;i++)
		if((p-1)%i==0){
			a[tot++]=i;
			if(i*i!=p-1)
				a[tot++]=(p-1)/i;
		}
	sort(a,a+tot);
	for(int i=0;i<tot;i++)
		if(a[i]!=1&&modpow(b,(p-1)/a[i],p)==1)return 0;
	return 1;
}
int main(){
	cin>>n>>x;
	n++;
	if(x==2)return puts("-1"),0;
	for(int i=2;i<n;i++)
		if(n%i==0)return puts("-1"),0;
	for(int i=x-1;i>=1;i--)
		if(check(i%n,n))
			return printf("%d\n",i),0;
	puts("-1");
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <unordered_set>
#include <climits>
#include <iomanip>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100000;
vector<ll> primes;
bitset<maxn> isprime;
void init(){
    isprime.reset(); isprime.flip();
    primes.pb(2ll);
    for(ll e = 4; e < maxn; e += 2) isprime[e] = 0;
    for(ll e = 3; e < maxn; e += 2) if(isprime[e]){
        primes.pb(e);
        for(ll f = e*e; f < maxn; f += e) isprime[f] = 0;
    }
}
bool isp(ll n){
    if(n == 2) return true;
    if(n < 2 || !(n&1)) return false;
    for(ll e = 3; e*e <= n; e += 2) if(n%e == 0) return false;
    return true;
}
ll solve(ll index, ll number){
    if(index < 0 || number < primes[index]) return 0ll;
    number /= primes[index];
    ll ans = number;
    for(int e = 0; e < index; e++) ans -= solve(e, number);
    return ans;
}
int main(){
    init();
    ll a, b, k; cin >> a >> b >> k;
    if(!isp(k)) cout << 0 << endl;
    else if(k > maxn) cout << (a <= k && k <= b) << endl;
    else{
        int index = lower_bound(primes.begin(), primes.end(), k) - primes.begin();
        cout << solve(index, b) - solve(index, a-1) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define MAX 1000003
#define pii pair<ll,ll>
#define VP vector< pii >
#define MOD  998244353
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(ll (i) = (a); (i) < (b); (i)++)
#define all(v) (v).begin(),(v).end()
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define SL(x) scanf("%lld",&(x))
#define SL2(x) scanf("%lld%lld",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define FF first
#define SS second
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m,k;
cin>>n>>m>>k;
vector<vector<ll>>dp(2005,vector<ll>(2005,0));
rep(i,1,n+1)
	dp[i][0]=m;
rep(i,2,n+1)
	{
		rep(j,1,i)
			{
				dp[i][j]=(dp[i-1][j-1]*(m-1)+dp[i-1][j])%MOD;
			}
	}
ll ans=dp[n][k]%MOD;
cout<<ans;
return 0;
}


#include <bits/stdc++.h>
#define re(i, x, y) for (int i = (x); i < (y); ++i)
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
#define repp(i, x, y, d) for (int i = (x); i <= (y); i += (d))
#define reep(i, x, y) for (int i = (x); i <= (y); i <<= 1)
#define pe(i, x, y) for (int i = (x) - 1; i >= (y); --i)
#define per(i, x, y) for (int i = (x); i >= (y); --i)
#define rep_e(i, u) for (int i = head[(u)]; i; i = e[i].nxt)
#define vi vector<int>
#define vL vector<LL>
#define vii vector<pii>
#define viL vector<piL>
#define vLi vector<pLi>
#define vLL vector<pLL>
#define eb emplace_back
#define pb pop_back
#define mp make_pair
#define pii pair<int, int>
#define piL pair<int, LL>
#define pLi pair<LL, int>
#define pLL pair<LL, LL>
#define lowbit(x) ((x) & (-(x)))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << endl
using namespace std;
typedef unsigned int ui;
typedef long long LL;
typedef unsigned long long ULL;
typedef double db;
#define getchar() (SB == TB && (TB = (SB = BB) + fread(BB, 1, 1 << 16, stdin), SB == TB) ? EOF : *SB++)
char BB[1 << 16], *SB = BB, *TB = BB;
template<typename T> void read(T &n) {
	T w = 1;
	n = 0;
	char ch = getchar();
	for ( ; !isdigit(ch); ch = getchar()) {
		if (ch == '-') {
			w = -1;
		}
	}
	for ( ; isdigit(ch); ch = getchar()) {
		n = n * 10 + (ch & 15);
	}
	n *= w;
}
template<typename T> void chkmn(T &a, const T &b) {
	(a > b) && (a = b);
}
template<typename T> void chkmx(T &a, const T &b) {
	(a < b) && (a = b);
}

int MOD;
int adt(const LL &a) {
	return (a % MOD + MOD) % MOD;
}
int inc(const int &a, const int &b) {
	return a + b >= MOD ? a + b - MOD : a + b;
}
int dec(const int &a, const int &b) {
	return a - b < 0 ? a - b + MOD : a - b;
}
int mul(const int &a, const int &b) {
	return 1LL * a * b % MOD;
}
int sqr(const int &a) {
	return 1LL * a * a % MOD;
}
void Adt(LL &a) {
	a = (a % MOD + MOD) % MOD;
}
void Inc(int &a, const int &b) {
	((a += b) >= MOD) && (a -= MOD);
}
void Dec(int &a, const int &b) {
	((a -= b) < 0) && (a += MOD);
}
void Mul(int &a, const int &b) {
	a = 1LL * a * b % MOD;
}
void Sqr(int &a) {
	a = 1LL * a * a % MOD;
}
int fsp(int a, int x = MOD - 2) {
	int res = 1;
	for ( ; x; x >>= 1, Sqr(a)) {
		if (x & 1) {
			Mul(res, a);
		}
	}
	return res;
}

const int maxn = 5e3 + 5;
int n;
int binom[maxn][maxn], pw[maxn][maxn], fac[maxn], fiv[maxn];

int main() {

    read(n), read(MOD);
    fac[0] = 1;
    rep(i, 1, n) {
        fac[i] = mul(fac[i - 1], i);
    }
    fiv[n] = fsp(fac[n]);
    per(i, n, 1) {
        fiv[i - 1] = mul(fiv[i], i);
    }
    rep(i, 0, n) {
        binom[i][0] = binom[i][i] = 1;
        re(j, 1, i) {
            binom[i][j] = inc(binom[i - 1][j], binom[i - 1][j - 1]);
        }
        pw[i][0] = 1;
        rep(j, 1, n) {
            pw[i][j] = mul(pw[i][j - 1], i);
        }
    }
    int ans = 0;
    re(i, 0, n) {
        re(j, 1, n - i) {
            Inc(ans, mul(mul(binom[n][i], mul(fac[n - i], mul(fiv[j], binom[n - i - 1 - j][j - 1]))), mul(pw[n - i - j][i], pw[n - 1][j])));
        }
    }
    printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>    // std::min
#include <cmath>        // std::abs

using namespace std;

int main() {
    //first make sure train is in correct direction. if not, see how long it takes for igor to walk to dest.
    //track current time, track wher is igor and where is train.
    //keep direction of train, end terminal, target terminal.
    int stationPos, igorStartPos, igorEndPos, igorCurrentPos;
    int tramSpeed, igorSpeed;
    int tramPos, tramDir;
    cin >> stationPos >> igorStartPos >> igorEndPos >> tramSpeed >> igorSpeed >> tramPos >> tramDir;

    int tramTime, igorTime;
    //check how long igor needs to get to igorEndPos
    igorTime = (abs (igorStartPos - igorEndPos)) * igorSpeed;

    if(igorSpeed < tramSpeed){
    	cout << igorTime;
    	return 0;
    }
    if(tramPos > igorStartPos){
    	if(tramDir == 1){
    		//finish tram route, then come back and pick igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)
    		tramTime = (stationPos - tramPos) + (stationPos - igorStartPos);
    		if(igorEndPos <= igorStartPos){
    			tramTime += (igorStartPos-igorEndPos);
    			tramTime *= tramSpeed;
    		}
    		else{
    			tramTime += (igorStartPos+igorEndPos);
    			tramTime *= tramSpeed;
    		}
    	}
    	else if(tramDir == -1){
    		//pick igor, then go to igorEndPos
    		tramTime = tramPos - igorStartPos;
    		if(igorEndPos >= igorStartPos){
    			tramTime += (igorStartPos + igorEndPos);
    			tramTime *= tramSpeed;
    		}
    		else{
    			tramTime += (igorStartPos-igorEndPos);
    			tramTime *= tramSpeed;
    		}
    	}
    }
    else if(tramPos <= igorStartPos){
    	if(tramDir == 1){
    		//pick up igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)
    		tramTime = igorStartPos - tramPos;
    		if(igorEndPos >= igorStartPos){
    			tramTime += (igorEndPos-igorStartPos);
    			tramTime *= tramSpeed;
    		}
    		else{
    			tramTime += (stationPos - igorStartPos + stationPos - igorEndPos);
    			tramTime *= tramSpeed;
    		}
    	}
    	else if(tramDir == -1){
    		//finish tram route, then come back and pick igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)
    		if(igorStartPos == tramPos)
    			tramTime = 0;
    		else
    			tramTime = tramPos + igorStartPos;
    		if(igorEndPos >= igorStartPos){
    			if(igorStartPos == tramPos)
    				tramTime += tramPos + igorEndPos;
    			else
    				tramTime += (igorEndPos - igorStartPos);
    			tramTime *= tramSpeed;
    		}
    		else{
    			if(igorStartPos == tramPos)
    				tramTime += (igorStartPos - igorEndPos);
    			else
    				tramTime += (stationPos - igorStartPos + stationPos - igorEndPos);
    			tramTime *= tramSpeed;
    		}
    	}
    }
    cout << min(igorTime, tramTime);
}


#include <iostream>
#include <cassert>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <vector>

#define forn(i, n) for(int i = 0; i < int(n); i++)
#define for1(i, n) for(int i = 1; i <= int(n); i++)
#define fore(i, l, n) for(int i = int(l); i < int(n); i++)
#define ford(i, n) for(int i = int(n) - 1; i >= 0; i--)

#define sz(s) (int)(s).size()
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long double ld;
typedef long long li;
typedef pair<int, int> pt;

const int N = 100000;
const ld EPS = 1e-9;

const int M = 1000 * 1000 * 1000 + 7;
int n;
li cnt[200];
int d[2][40000];
int c[200][200];
int C(int n, int k){
    if(k > n)
        return 0;
    if(k == n)
        return 1;
    if(k == 0)
        return 1;
    return (c[n-1][k]+c[n-1][k-1])%M;
}
void init(){
    forn(i, 200)
        forn(j, 200){
            c[i][j] = C(i, j);
        }
}
int mod_pow(int a, li b){
    int res = 1;
    while(b > 0){
        if(b&1)
            res = (res * 1LL*a)%M, b--;
        a = (a*1ll*a)%M;
        b >>= 1;
    }
    return res;
}
int mdp[200][200];
int main(){
   // freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    li m;
    int k;
    cin >>n >>m >> k;
    init();
    forn(i, n){
        cnt[i] = ((m-i)/n + ((m-i)%n!=0));

    }
    forn(i, n+1){
        d[0][i] = mod_pow(c[n][i], cnt[0]);
    }
    //cerr << clock() << endl;
    fore(i, 1, n)
        forn(cd, n+1)
            mdp[cd][i] = mod_pow(c[n][cd], cnt[i]);
    fore(i, 1, n){
        forn(ck, k+1){
            forn(cd, min(n, ck)+1){
                d[i&1][ck] = (d[i&1][ck] + d[(i-1)&1][ck-cd]*1LL*mdp[cd][i])%M;
            }
        }
        forn(ck, k+1)
            d[(i-1)&1][ck] = 0;
        //cerr << clock() << endl;
    }
    cout << d[(n-1)&1][k] << endl;
    return 0;
}


// LUOGU_RID: 90889303
#include <bits/stdc++.h>
using ll=long long;
using namespace std;
const int N = 1e6 + 10;
const ll P = 998244353, yg = 3;
ll k, fac[N], inv[N], f[N], g[N], S2[N],n;
ll bin[N];
ll qpow(ll a, ll b)
{
    ll res=1;
    while(b){
        if(b&1)res=res*a%P;
        a=a*a%P;
        b>>=1;
    }
    return res;
}
void NTT(ll *a, ll k, ll op)
{
    for (ll i = 0; i < k; i++)
        bin[i] = (bin[i >> 1] >> 1) | ((i & 1) * (k >> 1));
    for (ll i = 0; i < k; i++)
        if (i < bin[i])
            swap(a[i], a[bin[i]]);
    for (ll i = 1; i < k; i <<= 1)
    {
        ll wn = qpow(yg, op == 1 ? (P - 1) / (2 * i) : (P - 1) - (P - 1) / (2 * i)), w, t;
        for (ll j = 0; j < k; j += i << 1)
        {
            w = 1;
            for (ll k = 0; k < i; k++)
            {
                t = a[i + j + k] * w % P;
                w = w * wn % P;
                a[i + j + k] = (a[j + k] - t + P) % P;
                a[j + k] = (a[j + k] + t) % P;
            }
        }
    }
    if (op == -1)
    {
        ll Inv = qpow(k, P - 2);
        for (ll i = 0; i < k; i++)
            a[i] = a[i] * Inv % P;
    }
}
void get_s2(){
    fac[0] = inv[0] = 1;
    for (int i = 1; i <= k; i++)
        fac[i] = fac[i - 1] * i % P, inv[i] = qpow(fac[i], P - 2);
    for (int i = 0; i <= k; i++)
        f[i] = (qpow(-1, i) + P) % P * inv[i] % P;
    for (int i = 0; i <= k; i++)
        g[i] = qpow(i, k) * inv[i] % P;
    ll len = 1;
    while (len < (k + 1) << 1)
        len <<= 1;
    NTT(f, len, 1);
    NTT(g, len, 1);
    for (int i = 0; i < len; i++)
        S2[i] = (f[i] * g[i]) % P;
    NTT(S2, len, -1);
}
int main()
{
    cin >> n >> k;
    get_s2();
    ll ans=0;
    ll base=n*qpow(n+1,n-1)%P;
    ll inv=qpow(n+1,P-2);
    // for(int i=1;i<=k;i++)cout<<S2[i]<<endl;
    for(ll i=1;i<=min(n,k);i++){
        ans=(ans+S2[i]*base)%P;
        base=base*inv%P*(n-i)%P;
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<sstream>

using namespace std;
#define MAX_N 1000005
#define inf 0x3fffffff
#define LL long  long
#define ull unsigned long long
const LL INF = 1e18;
const double eps = 1e-8;
//const double pi = acos(-1);
const LL mod = 1e9+7;
typedef pair<LL, LL>P;

P ans;
LL pow(LL m)
{
    LL low = 1;
    LL up = 1e5;
    LL ans = 0;
    while(low <= up) {
        LL mid = (low+up)/2;
        if(mid*mid*mid<=m) {
            ans = max(ans, mid);
            low = mid + 1;
        }
        else {
            up = mid - 1;
        }
    }
    return ans;
}
void dfs(LL m, LL cnt, LL sum)
{
    if(m <= 7) {
        ans = max(ans, P(cnt+m, sum+m));
        return;
    }
    LL p = pow(m);
    dfs(m-p*p*p, cnt+1, sum+p*p*p);
    dfs(p*p*p-1-(p-1)*(p-1)*(p-1), cnt+1, sum+(p-1)*(p-1)*(p-1));
}
int main()
{
    LL m;
    cin >> m;
    dfs(m, 0, 0);
    cout << ans.first << " " << ans.second;
}


#include <iostream>

using namespace std;
typedef long long ll;
const int mod=1e9+7;;
ll tmp;
ll qmi(ll a,ll b=mod-2){
    ll ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }return ans;
}
struct sq{
    ll a,b;
    sq(ll x,ll y){a=x,b=y;}
};
ll Mod(ll x){
    return (x%mod+mod)%mod;
}
inline sq operator+(const sq&l,const sq&r){return sq{Mod(l.a+r.a),Mod(l.b+r.b)};}
inline sq operator-(const sq&l,const sq&r){return sq{Mod(l.a-r.a),Mod(l.b-r.b)};}
inline sq operator*(const sq&l,const sq&r){return sq{Mod(l.a*r.a%mod+l.b*r.b*tmp%mod),Mod(l.a*r.b%mod+l.b*r.a%mod)};}

inline sq inv(const sq&l){
    ll c=(l.a*l.a%mod-l.b*l.b%mod*tmp%mod)%mod;
    if(c<0)c+=mod;
    c=qmi(c,mod-2);
    // cout<<c<<endl;
    return {Mod(l.a*c%mod),Mod((mod-c)*l.b%mod)};
}
inline sq qs(sq l,ll b=mod-2){
    sq ans={1,0};
    while(b){
        if(b&1)ans=ans*l;
        l=l*l;
        b>>=1;
    }return ans;
}
ll s[510][510];
ll p[510];
ll iv[510];
ll _iv[510];
void init(){
    s[0][0]=1;
    for(int i=1;i<=300;i++){
        s[i][0]=0;
        for(int j=1;j<=i;j++){
            s[i][j]=(s[i-1][j-1]+((i-1)*s[i-1][j])%mod)%mod;
        }
    }
    p[0]=p[1]=iv[0]=iv[1]=_iv[0]=_iv[1]=1;
    for(int i=2;i<=300;i++){
        p[i]=p[i-1]*i%mod;
        iv[i]=(mod-mod/i)*iv[mod%i]%mod;
        _iv[i]=_iv[i-1]*iv[i]%mod;
    }
}
ll k,l,r;
ll C(ll a,ll b){
    if(b>a)return 0;
    return p[a]*_iv[b]%mod*_iv[a-b]%mod;
}
sq sum(sq x,ll t){
    sq t1=qs(x,t+1);
    t1=t1-x;
    sq t2=x-sq{1,0};
    // cout<<x.a<<' '<<x.b<<' '<<t<<endl;
    // cout<<t1.a<<' '<<t2.a<<' '<<(inv(t2)).a<<endl;
    return t1*inv(t2);
}
sq get_ans(sq x,ll l,ll r){
    if(x.a==1&&x.b==0)return x*sq{r-l+1,0};
    return sum(x,r)-sum(x,l-1);
}
int main()
{
    tmp=5;
    cin>>k>>l>>r;
    l+=2,r+=2;
    init();
    // cout<<iv[3]<<endl;
    sq res(0,0);
    sq A(0,iv[5]);
    sq B(0,mod-iv[5]);
    sq a(iv[2],iv[2]);
    sq b(iv[2],mod-iv[2]);
    ll f=k%2?-1:1;
    for(int i=0;i<=k;i++){
        sq qwq(0,0);
        for(int j=0;j<=i;j++){
            sq qaq=sq(C(i,j),0)*qs(A,j)*qs(B,i-j)*get_ans(qs(a,j)*qs(b,i-j),l,r);
            // cout<<C(i,j)<<' '<<qs(A,j).a<<' '<<qs(B,i-j).a<<' '<<get_ans(qs(a,j)*qs(b,i-j),l,r).a<<endl;
            qwq=qwq+qaq;
        }

        res=res+sq{f,0}*qwq*sq{s[k][i],0};
        f*=-1;
    }
    res.a=res.a*qmi(p[k],mod-2)%mod;
    cout<<res.a<<endl;
}


#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define pb push_back
#define MP std::make_pair
#define PII std::pair<int, int>
#define all(x) (x).begin(), (x).end()
#define CL(a, b) memset(a, b, sizeof a)
#define rep(i, l, r) for (int i = (l); i <= (r); ++ i)
#define per(i, r, l) for (int i = (r); i >= (l); -- i)
#define PE(x, a) for (int x = head[a]; x;x = edge[x].next)
#define id(x, y) idx[x + 50][y + 50]

typedef long long ll;

const int MAXN = 1e2 + 7;
const ll HA = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n;
int idx[MAXN][MAXN], cnt, a[8003][8003];
ll p[4];

ll fp(ll a, ll b = HA - 2) {
    ll r = 1;
    while (b) {
        if (b & 1) r = r * a % HA;
        b >>= 1, a = a * a % HA;
    }
    return r;
}

void gauss() {
    n <<= 1;
    rep(i, 1, cnt) {
        ll d = fp(1ll * a[i][i]);
        for (int j = i, t = 0; j <= cnt && t <= n; t++, j++) a[i][j] = 1ll * a[i][j] * d % HA;
        a[i][0] = 1ll * a[i][0] * d % HA;
        for (int j = i + 1, t1 = 0; j <= cnt && t1 <= n; j++, t1++) {
            d = a[j][i];
            if (!d) continue;
            for (int k = i, t2 = 0; k <= cnt && t2 <= n; k++, t2++) a[j][k] = 1ll * (1ll * a[j][k] + HA - 1ll * d * a[i][k] % HA) % HA;
            a[j][0] = 1ll * (1ll * a[j][0] + HA - 1ll * d * a[i][0] % HA) % HA;

        }
    }
    per(i, cnt, 1) rep(j, i + 1, cnt) a[i][0] = 1ll * (1ll * a[i][0] + HA - 1ll * a[j][0] * a[i][j] % HA) % HA;
}

int main() {
    scanf("%d", &n);
    ll sum = 0;
    rep(i, 0, 3) scanf("%lld", p + i), sum = (sum + p[i]) % HA;
    sum = fp(sum);
    rep(i, 0, 3) p[i] = p[i] * sum % HA;
    rep(x, -n, n) per(y, n, -n) if (x * x + y * y <= n * n) id(x, y) = ++cnt;
    rep(x, -n, n) per(y, n, -n) {
        if (int f = id(x, y)) {
            a[f][f] = -1; a[f][0] = -1;
            int t;
            rep(k, 0, 3) {
                int tx = x + dx[k];
                int ty = y + dy[k];
                if (id(tx, ty)) a[f][id(tx, ty)] = p[k];
            }
        }
    }
    gauss();
    printf("%lld\n", 1ll * (a[id(0, 0)][0] % HA + HA) % HA);
    return 0;
}


#include<bits/stdc++.h>
#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)
#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)
using namespace std;
const int maxn=2.7e5+5;
int m,n,k;
int mod;
int f[19][maxn];
vector<long long> g;
int cnt[maxn];
int upd(const int &x){
	if(x<0)return x+mod;
	if(x>=mod)return x-mod;
	return x;
}
void add(int &x,const int &y){
	x=upd(x+y);
}
int ksm(int x,int k){
	int ans=1;
	while(k){
		if(k&1)ans=1ll*ans*x%mod;
		x=1ll*x*x%mod;
		k>>=1;
	}
	return ans;
}
int jc[maxn],inv[maxn];
void init(int n){
	jc[0]=1;
	fr(i,1,n)jc[i]=1ll*jc[i-1]*i%mod;
	inv[n]=ksm(jc[n],mod-2);
	fd(i,n-1,0)inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
void F(int *f,int n,int h){
	for(int p=1;p<n;p<<=1)
		for(int i=0;i<n;i+=p<<1)fr(k,i,i+p-1){
			add(f[k+p],h*f[k]);
		}
}
int l;
vector<long long> operator *(vector<long long> &A,vector<long long> &B){
	static vector<long long> C;C.clear();C.resize(l+1);
	fr(i,0,l)fr(j,0,l-i)C[i+j]+=A[i]*B[j];
	fr(i,0,l)C[i]%=mod;
	return C;
}
vector<long long> pksm(vector<long long> A,int k){
	static vector<long long> C;C.clear();C.resize(l+1);
	C[0]=1;
	while(k){
		if(k&1)C=C*A;
		A=A*A;
		k>>=1;
	}
	return C;
}
int main(){
#ifdef pig
	freopen("C.in","r",stdin);
	freopen("C.out","w",stdout);
#endif
	cin>>n>>k>>mod;
	init(n);
	if(n&1){
		cout<<ksm(k,n);
		return 0;
	}
	m=1;while(m<=n)m<<=1,l++;
	fr(i,0,m-1)cnt[i]=cnt[i>>1]+(i&1);
	init(m);
	fr(i,0,m-1){
		f[cnt[i]][i]=inv[i];
	}
	fr(i,0,l)F(f[i],m,1);
	g.resize(l+1);
	fr(s,0,m-1){
		fr(i,0,l)g[i]=f[i][s];
		g=pksm(g,k);
		fr(i,0,l)f[i][s]=g[i];
	}
	F(f[cnt[n]],m,-1);
	cout<<upd(ksm(k,n)-1ll*f[cnt[n]][n]*jc[n]%mod);
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
int n,k,mod,dp[18][260000],list[33],cnt_list,bi[33],cnt,fac[260000],inv[260000],len[260000];
bool vis[260000];
int power(int a,int n)
{
	if(n==0)
	   return 1;
    int ret=power(a,n/2);
    ret=1LL*ret*ret%mod;
    if(n%2)
       ret=1LL*ret*a%mod;
    return ret;
}
int main()
{
	int i,j,s,p,q,flag;
	scanf("%d%d%d",&n,&k,&mod);
    if(n%2)
        printf("%d\n",power(k,n));
    else
    {
    	for(i=0;i<=n;i++)
    	{
	    	if(i==0)
	    	   fac[i]=1;
  	        else
  	           fac[i]=1LL*fac[i-1]*i%mod;
	    }
	    inv[n]=power(fac[n],mod-2);
	    for(i=n-1;i>=0;i--)
	       inv[i]=1LL*inv[i+1]*(i+1)%mod;
    	flag=0;
    	cnt_list=0;
		for(i=0;(1<<i)<=n;i++)
    	{
	    	if(n&(1<<i))
	    	   list[cnt_list++]=i;
	    }
	    dp[0][0]=1;
	    for(i=0;i<=n;i++)
	    {
    		len[i]=0;
    		j=i;
    		while(j)
    		{
		    	len[i]++;
		    	j=(j-1)&j;
		    }
    	}
    	for(i=n;;i=(i-1)&n)
    	{
    		vis[i]=true;
	    	if(i==0)
	    	   break;
	    }
	    for(i=1;i<=n;i++)
	    {
	        if(vis[i])
	        {
        		for(j=i;j>0;j=(j-1)&i)
        		{
        			int l=min(k,len[i-j]+1);
	    	    	 for(s=1;s<=l;s++)
	    	    	 {
	    	    	 	 if(dp[s-1][i-j])
 	        			     dp[s][i]=(dp[s][i]+1LL*dp[s-1][i-j]*inv[j])%mod;
	    	    	 }
				}
	        }
    	}
    	int ans=0;
    	for(i=0;i<=min(k,len[n]);i++)
    	{
	    	if(dp[i][n])
	    	{
	    		int tmp=1LL*dp[i][n]*fac[k]%mod;
	    		tmp=1LL*tmp*inv[k-i]%mod;
	    		tmp=1LL*tmp*inv[i]%mod;
	    		tmp=1LL*tmp*fac[n]%mod;
	    		ans=(ans+tmp)%mod;
	    	}
	    }
    	printf("%d\n",(power(k,n)-ans+mod)%mod);
    }
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

typedef long long ll;

const int N=262144;

int L,n,k,Mo,len,bit[20],fac[N],inv[N],cnt[N],g[20][N],f[20][N],h[20],p[20];

int pwr(int x,int y) {
	int z=1;
	for(;y;y>>=1,x=(ll)x*x%Mo)
		if (y&1) z=(ll)z*x%Mo;
	return z;
}

void inc(int &x,int y) {x=x+y>=Mo?x+y-Mo:x+y;}
void dec(int &x,int y) {x=x-y<0?x-y+Mo:x-y;}
void FWT(int *a,int len) {for(int i=1;i<len;i<<=1) for(int j=0;j<len;j+=i<<1) for(int k=0;k<i;k++) inc(a[j+k+i],a[j+k]);}
void IFWT(int *a,int len) {for(int i=1;i<len;i<<=1) for(int j=0;j<len;j+=i<<1) for(int k=0;k<i;k++) dec(a[j+k+i],a[j+k]);}

void get_ln(int *a,int *b,int n) {
	b[0]=0;
	fo(i,1,n) {
		b[i]=(ll)a[i]*i%Mo;
		fo(j,1,i-1) (b[i]+=Mo-(ll)b[j]*a[i-j]%Mo*j%Mo)%=Mo;
		b[i]=(ll)b[i]*pwr(i,Mo-2)%Mo;
	}
}

void get_exp(int *a,int *b,int n) {
	b[0]=1;
	fo(i,1,n) {
		b[i]=0;
		fo(j,1,i) (b[i]+=(ll)a[j]*b[i-j]%Mo*j%Mo)%=Mo;
		b[i]=(ll)b[i]*pwr(i,Mo-2)%Mo;
	}
}

int main() {
	scanf("%d%d%d",&L,&k,&Mo);
	int all=pwr(k,L);
	if (L&1) {
		printf("%d\n",all);
		return 0;
	}
	fac[0]=1;fo(i,1,L) fac[i]=(ll)fac[i-1]*i%Mo;
	inv[L]=pwr(fac[L],Mo-2);fd(i,L-1,0) inv[i]=(ll)inv[i+1]*(i+1)%Mo;
	fo(s,1,L) cnt[s]=cnt[s^(s&-s)]+1;
	fo(i,0,18) if (L>>i&1) bit[++n]=1<<i;
	len=1<<n;
	fo(i,0,len-1) {
		int s=0;
		fo(j,1,n) if (i>>(j-1)&1) s+=bit[j];
		g[cnt[i]][i]=inv[s];
	}
	fo(i,0,n) FWT(g[i],len);
	fo(s,0,len-1) {
		fo(i,0,n) h[i]=g[i][s];
		get_ln(h,p,n);
		fo(i,0,n) p[i]=(ll)p[i]*k%Mo;
		get_exp(p,h,n);
		fo(i,0,n) f[i][s]=h[i];
	}
	IFWT(f[n],len);
	int ans=(ll)f[n][len-1]*fac[L]%Mo;
	ans=(all-ans+Mo)%Mo;
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define inf 17446744073709551616ull
#define ll unsigned long long
using namespace std;
ll mod,n,k,dp[18][250005];
ll fac[250005],inv[250005];
inline ll qpow(ll a,ll b)
{ll res=1ll,tp=a;
while (b)
{if (b&1ll) {res*=tp;res%=mod;}
tp*=tp;tp%=mod;b>>=1ll;
}
return res;
}
inline ll cal(int a,int b)
{return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;}
inline ll calc()
{int i,j,l;fac[0]=1ll;
for (i=1;i<=n;i++) fac[i]=fac[i-1]*((ll)(i))%mod;
inv[n]=qpow(fac[n],mod-2ll);
for (i=n-1;i>=0;i--) inv[i]=inv[i+1]*((ll)(i+1))%mod;
for (i=1;i<=n;i++) dp[1][i]=inv[i];
ll ans=dp[1][n]*cal(k,1)%mod;
int t=min(17,((int)(k)));
for (i=2;i<=t;i++)
{for (j=n;j;j=(j-1)&n)
{if (!dp[i-1][j]) {continue;}
int r=n^j;
for (l=r;l;l=(l-1)&r)
{dp[i][j+l]+=dp[i-1][j]*inv[l];
if (dp[i][j+l]>inf) dp[i][j+l]%=mod;
}
}
for (j=n;j;j=(j-1)&n) dp[i][j]%=mod;
ans+=dp[i][n]*cal(k,i);ans%=mod;
}
ans*=fac[n];ans%=mod;
return ans;
}
int main (){
	cin>>n>>k>>mod;
	ll ans=qpow(k,n);
	if (n%2==0) ans=(ans-calc()+mod)%mod;
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
int n,k,mod,dp[18][260000],list[33],cnt_list,bi[33],cnt,fac[260000],inv[260000],len[260000];
bool vis[260000];
int power(int a,int n)
{
	if(n==0)
	   return 1;
    int ret=power(a,n/2);
    ret=1LL*ret*ret%mod;
    if(n%2)
       ret=1LL*ret*a%mod;
    return ret;
}
int main()
{
	int i,j,s,p,q,flag;
	scanf("%d%d%d",&n,&k,&mod);
    if(n%2)
        printf("%d\n",power(k,n));
    else
    {
    	for(i=0;i<=n;i++)
    	{
	    	if(i==0)
	    	   fac[i]=1;
  	        else
  	           fac[i]=1LL*fac[i-1]*i%mod;
	    }
	    inv[n]=power(fac[n],mod-2);
	    for(i=n-1;i>=0;i--)
	       inv[i]=1LL*inv[i+1]*(i+1)%mod;
    	flag=0;
    	cnt_list=0;
		for(i=0;(1<<i)<=n;i++)
    	{
	    	if(n&(1<<i))
	    	   list[cnt_list++]=i;
	    }
	    dp[0][0]=1;
	    for(i=0;i<=n;i++)
	    {
    		len[i]=0;
    		j=i;
    		while(j)
    		{
		    	len[i]++;
		    	j=(j-1)&j;
		    }
    	}
    	for(i=n;;i=(i-1)&n)
    	{
    		vis[i]=true;
	    	if(i==0)
	    	   break;
	    }
	    for(i=1;i<=n;i++)
	    {
	        if(vis[i])
	        {
        		for(j=i;j>0;j=(j-1)&i)
        		{
	    	    	 for(s=1;s<=min(k,len[i-j]+1);s++)
 	        		 {
 	        			  dp[s][i]+=1LL*dp[s-1][i-j]*inv[j]%mod;
		      	 	      dp[s][i]%=mod;
 	        		 }
		        }
	        }
    	}
    	int ans=0;
    	for(i=0;i<=min(k,len[n]);i++)
    	{
	    	if(dp[i][n])
	    	{
	    		int tmp=1LL*dp[i][n]*fac[k]%mod;
	    		tmp=1LL*tmp*inv[k-i]%mod;
	    		tmp=1LL*tmp*inv[i]%mod;
	    		tmp=1LL*tmp*fac[n]%mod;
	    		ans=(ans+tmp)%mod;
	    	}
	    }
    	printf("%d\n",(power(k,n)-ans+mod)%mod);
    }
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N=250005,M=1<<17,D=18;

int P;
int power(int a,int t){
	int r=1;
	while(t){
		if(t&1)r=(ll)r*a%P;
		a=(ll)a*a%P;t>>=1;
	}
	return r;
}

int L,K,fac[N],n,m,bit[20],ifac[N],bitcnt[N];
ll suma[D][M],sumc[D][M];
ull dp[D][M],tmp[D][M];

int main(){

	scanf("%d%d%d",&L,&K,&P);
	fac[0]=1;
	for(int i=1;i<=L;i++){
		fac[i]=(ll)fac[i-1]*i%P;
		bitcnt[i]=bitcnt[i>>1]+(i&1);
	}
	ll all=power(K,L);
	if(L&1)printf("%lld\n",all),exit(0);

	ifac[L]=power(fac[L],P-2);
	for(int i=L;i>=1;i--)ifac[i-1]=(ll)ifac[i]*i%P;
	for(int i=1;(1<<i)<=L;i++)if(L>>i&1)bit[n++]=1<<i;

	m=1<<n;
	// build a
	for(int i=0;i<m;i++){
		int s=0;
		for(int j=0;j<n;j++)if(i>>j&1)s+=bit[j];
		suma[bitcnt[i]][i]=ifac[s];
	}

	// get suma
	for(int t=0;t<=n;t++){
		int i,j,k,l;
		ll*a=suma[t];
		for(l=m;l>=2;l>>=1)for(j=0,i=l>>1;j<m;j+=l)for(k=0;k<i;k++)a[j+k+i]+=a[j+k];
		for(j=0;j<m;j++)a[j]=a[j]%P;
	}

	for(int t=0;t<=n;t++)memcpy(dp[t],suma[t],m<<3);
	int cc=0,revK=0;
	while(K)(revK<<=1)|=K&1,cc++,K>>=1;
	for(cc--,revK>>=1;cc;cc--,revK>>=1){

		// use dp to update dp
		// f i,j means V = i, P = j
		// f (a + b),j = ∑g a,j * h b,j

		for(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);
		for(int a=0;a<=n;a++)for(int b=0;a+b<=n;b++){
			ull*f=tmp[a+b],*g=dp[a],*h=dp[b];
			for(int j=0;j<m;j++)f[j]+=g[j]*h[j];
		}
		for(int t=0;t<=n;t++)for(int j=0;j<m;j++)dp[t][j]=tmp[t][j]%P;

		if(revK&1){
			// use suma to update dp

			for(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);
			for(int a=0;a<=n;a++)for(int b=0;a+b<=n;b++){
				ull*f=tmp[a+b],*g=dp[a]; ll*h=suma[b];
				for(int j=0;j<m;j++)f[j]+=g[j]*h[j];
			}
			for(int t=0;t<=n;t++)for(int j=0;j<m;j++)dp[t][j]=tmp[t][j]%P;

		}
	}

	// get answer
	for(int t=0;t<=n;t++)memcpy(sumc[t],dp[t],m<<3);
	for(int t=0;t<=n;t++){
		int i,j,k,l;
		ll*a=sumc[t];
		for(l=2;l<=m;l<<=1)for(j=0,i=l>>1;j<m;j+=l)for(k=0;k<i;k++)a[j+k+i]-=a[j+k];
		for(j=0;j<m;j++)a[j]=(a[j]%P+P)%P;
	}

	ll ans=(all-sumc[n][(1<<n)-1]*fac[L]%P+P)%P;
	printf("%lld\n",ans);

	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
LL f[30][300010],ans,fac[300010],inv[300010];
int N,n,k,MO,s,p[1000010],a[55],S[1000010];
LL pw(LL x,int y){
	LL t=1;
	for (;y;y>>=1){
		if (y&1) t=t*x%MO;
		x=x*x%MO;
	}
	return t;
}
LL P(int x,int y){
	return fac[x]*inv[x-y]%MO;
}
int main(){
	scanf("%d%d%d",&n,&k,&MO);
	if (n&1) return cout<<pw(k,n)<<endl,0;
	fac[0]=1;
	FOR(i,1,n) fac[i]=fac[i-1]*i%MO;
	inv[n]=pw(fac[n],MO-2);
	FORD(i,n-1,0) inv[i]=inv[i+1]*(i+1)%MO;
	int t=n;
	FOR(i,0,20){
		if (t&1) a[s++]=1<<i;
		t>>=1;
	}
	N=1<<s;
	FOR(i,0,N-1)
		FOR(j,0,s-1)
			if ((i>>j)&1) S[i]+=a[j];
	FOR(i,0,N-1){
		int t=-1;
		FOR(j,0,20)
			if ((i>>j)&1) t=j;
		p[i]=(1<<(t+1))+i;
	}
	f[0][0]=1;
	FOR(i,0,k-1)
		FOR(j,0,N-1)
			if (f[i][j]){
				for (int k=p[j];k<N;k=(k+1)|j)
					(f[i+1][k]+=f[i][j]*inv[S[k^j]])%=MO;
			}
	FOR(i,0,k) (ans+=f[i][N-1]*P(k,i))%=MO;
	ans=ans*fac[n]%MO;
	ans=(pw(k,n)-ans+MO)%MO;
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << " ";
#define endln cerr << "\n";

void transform(int a[], int n, int p, int invert) {
    int k = __lg(n);
    FOR(i, 0, k) {
        FOR(msk, 0, n) if (bit(msk, i)) {
            int pmsk = msk ^ (1 << i);
            if (!invert) {
                addmod(a[msk], a[pmsk], p);
            }
            else {
                submod(a[msk], a[pmsk], p);
            }
        }
    }
}

vector<int> pow_convol(vector<int> a, int k, int p) {
    int n = a.size();
    if (k == 1) {
        return a;
    }
    else if (k & 1) {
        vector<int> r = pow_convol(a, k - 1, p);
        vector<int> res(n, 0);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                res[i + j] = (res[i + j] + (long long) r[i] * a[j]) % p;
            }
        }
        return res;
    }
    else {
        vector<int> r = pow_convol(a, k >> 1, p);
        vector<int> res(n, 0);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                res[i + j] = (res[i + j] + (long long) r[i] * r[j]) % p;
            }
        }
        return res;
    }
}

vector<int> subset_pow_convol(vector<int> a, int k, int p) {
    static const int logn = 19;
    static int fa[logn][1 << logn];
    static int fb[logn][1 << logn];
    static int fc[logn][1 << logn];
    int n = a.size();
    while (n != (n & -n)) n += n & -n;
    vector<int> res(n, 0);
    for (int i = 0; i < logn; i++) {
        for (int j = 0; j < n; j++) {
            if (bitcount(j) == i) {
                fa[i][j] = j < sz(a) ? a[j] : 0;
            }
            else {
                fa[i][j] = 0;
            }
            fc[i][j] = 0;
        }
        transform(fa[i], n, p, 0);
    }
    FOR(i, 0, n) {
        vi va(logn);
        FOR(j, 0, logn) {
            va[j] = fa[j][i];
        }
        va = pow_convol(va, k, p);
        FOR(j, 0, logn) {
            fc[j][i] = va[j];
        }
    }
    for (int i = 0; i < logn; i++) {
        transform(fc[i], n, p, 1);
        for (int j = 0; j < n; j++) if (bitcount(j) == i) {
            res[j] = fc[i][j];
        }
    }
    return res;
}

const int maxn = 1e6 + 5;
int n, k, p;
int fac[maxn];
int ifac[maxn];
int dp[2][maxn];
int f[maxn];

void solve() {
    cin >> n >> k >> p;
    int nn = n;
    FOR(i, 0, n + 1) if ((n & i) == i) {
        int t = 0;
        FOR(j, 0, 20) if (bit(n, j)) {
            t <<= 1;
            t |= bit(i, j);
        }
        f[t] = i;
        nn = t;
    }
    if (n & 1) {
        cout << fpow(k, n, p) << "\n";
        return;
    }
    fac[0] = 1; FOR(i, 1, maxn) fac[i] = mult(fac[i - 1], i, p);
    FOR(i, 0, maxn) ifac[i] = inv(fac[i], p);
    vi a(nn + 1, 0);
    FOR(i, 0, nn + 1) a[i] = ifac[f[i]];
    int res = fpow(k, n, p);
    a = subset_pow_convol(a, k, p);
    submod(res, mult(a[nn], fac[n], p), p);
    cout << res << "\n";
}

int main() {
    int JUDGE_ONLINE = 1;
    if (fopen("in.txt", "r")) {
        JUDGE_ONLINE = 0;
        assert(freopen("in.txt", "r", stdin));
        //assert(freopen("out.txt", "w", stdout));
    }
    else {
        ios_base::sync_with_stdio(0), cin.tie(0);
    }
    solve();
    if (!JUDGE_ONLINE) {
        //cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int Maxn = 1 << 19 | 5;
int n, k, p, maxi, ct[Maxn], f[Maxn];
int tmp1[20][Maxn], tmp2[20][Maxn], tmp3[20][Maxn];
long long pp;
long long t[20][Maxn];
long long fac[Maxn], inv[Maxn];
long long fast_pow(long long x, long long y)
{
    long long ans = 1, now = x;
    while (y)
    {
        if (y & 1) ans = ans * now % p;
        now = now * now % p;
        y >>= 1;
    }
    return ans;
}
void init(void)
{
    fac[0] = 1;
    for (int i = 1; i <= maxi; i++)
        fac[i] = fac[i - 1] * i % p;
    inv[maxi] = fast_pow(fac[maxi], p - 2);
    for (int i = maxi - 1; i >= 0; i--)
        inv[i] = inv[i + 1] * (i + 1) % p;
}
int lower(int x)
{
    int tmp = 1;
    for (; tmp < x; tmp <<= 1);
    return tmp;
}
void FWT(int *now, int len, bool type = false)
{
    long long *T = t[0];
    for (int i = 0; i < len; i ++) T[i] = now[i];
    if (type)
        for (int i = 1; i < len; i <<= 1)
            for (int j = 0; j < len; j += (i << 1))
                for (int k = j; k < i + j; k++)
                    T[i + k] -= T[k];
    else
        for (int i = 1; i < len; i <<= 1)
            for (int j = 0; j < len; j += (i << 1))
                for (int k = j; k < i + j; k++)
                    T[i + k] += T[k];
    for (int i = 0; i < len; i ++) {
        now[i] = T[i] % p;
        if (now[i] < 0) now[i] += p;
    }
}
inline void __a (long long &x) { x >= pp ? x -= pp : x; }
void multi(void)
{
    for (int j = 0; j <= 19; j++)
    {
        memset(tmp1[j], 0, sizeof(int[maxi + 1]));
        memset(t[j], 0, sizeof(long long[maxi + 1]));
    }
    for (int j = 0; j <= maxi; j++)
        tmp1[ct[j]][j] = f[j];
    for (int j = 0; j <= 19; j++)
        FWT(tmp1[j], maxi + 1);
    for (int q = 0; q <= 19; q++)
        for (int l = 0; q + l <= 19; l++)
            for (int s = 0; s <= maxi; s++)
                __a(t[q + l][s] += 1ll * tmp1[q][s] * tmp2[l][s]);
    for (int q = 0; q <= 19; q++)
        for (int s = 0; s <= maxi; s++)
            tmp3[q][s] = t[q][s] % p;
    for (int j = 0; j <= 19; j++)
        FWT(tmp3[j], maxi + 1, true);
    for (int j = 0; j <= maxi; j++)
        f[j] = tmp3[ct[j]][j];
}
void multi2(void)
{
    static int tmp[20][Maxn];
    for (int i = 0; i <= 19; i++)
        memset(t[i], 0, sizeof(long long[maxi + 1]));
    for (int q = 0; q <= 19; q++)
        for (int l = 0; q + l <= 19; l++)
            for (int s = 0; s <= maxi; s++)
                __a(t[q + l][s] += 1ll * tmp2[q][s] * tmp2[l][s]);
    for (int q = 0; q <= 19; q++)
        for (int s = 0; s <= maxi; s++)
            tmp[q][s] = t[q][s] % p;
    for (int i = 0; i <= 19; i++)
        FWT(tmp[i], maxi + 1, true);
    for (int i = 0; i <= 19; i++)
        memset(tmp2[i], 0, sizeof(int[maxi + 1]));
    for (int i = 0; i <= maxi; i++)
        tmp2[ct[i]][i] = tmp[ct[i]][i];
    for (int i = 0; i <= 19; i++)
        FWT(tmp2[i], maxi + 1);
}
int main()
{
    scanf("%d%d%d", &n, &k, &p);
    pp = 1ll * p * p;
    if (n & 1)
    {
        printf("%lld", fast_pow(k, n));
        return 0;
    }
    f[0] = 1;
    maxi = lower(n + 1) - 1;
    init();
    for (int i = 0; i <= maxi; i++)
        ct[i] = __builtin_popcount(i);
    for (int i = 0; i <= maxi; i++)
        tmp2[ct[i]][i] = inv[i];
    for (int i = 0; i <= 19; i++)
        FWT(tmp2[i], maxi + 1);
    int res_k = k;
    while (k)
    {
        cerr << k << endl;
        if (k & 1) multi();
        k >>= 1;
        if (k) multi2();
    }
    printf("%lld", (fast_pow(res_k, n) - fac[n] * f[n] % p + p) % p);
    return 0;
}


#include <bits/stdc++.h>
#define inf 17446744073709551616ull
#define ll unsigned long long
using namespace std;
ll mod,dp[18][250005];
ll fac[250005],inv[250005];
ll n,k;
inline ll qpow(ll a,ll b)
{ll res=1ll,tp=a;
while (b)
{if (b&1ll) {res*=tp;res%=mod;}
tp*=tp;tp%=mod;b>>=1ll;
}
return res;
}
inline ll cal(int a,int b)
{return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;}
inline ll calc()
{int i,j,l;
fac[0]=1ll;
for (i=1;i<=n;i++) fac[i]=fac[i-1]*((ll)(i))%mod;
inv[n]=qpow(fac[n],mod-2ll);
for (i=n-1;i>=0;i--) inv[i]=inv[i+1]*((ll)(i+1))%mod;
for (i=1;i<=n;i++) {dp[1][i]=inv[i];}
ll ans=dp[1][n]*cal(k,1)%mod;
int t=min(17,((int)(k)));
for (i=2;i<=t;i++)
{for (j=n;j;j=(j-1)&n)
{for (l=j;l;l=(l-1)&j)
{dp[i][j]+=dp[i-1][j^l]*inv[l];
if (dp[i][j]>inf)
dp[i][j]%=mod;
}
dp[i][j]%=mod;
}
ans+=dp[i][n]*cal(k,i);
ans%=mod;
}
ans*=fac[n];ans%=mod;
return ans;
}
int main (){
	cin>>n>>k>>mod;
	ll ans=qpow(k,n);
	if (n&1)
	{cout<<ans<<endl;
	return 0;
	}
	ans=(ans-calc()+mod)%mod;
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

typedef long long ll;

const int N=262144;

int L,n,k,Mo,len,bit[20],fac[N],inv[N],cnt[N],g[20][N],f[20][N];

int pwr(int x,int y) {
	int z=1;
	for(;y;y>>=1,x=(ll)x*x%Mo)
		if (y&1) z=(ll)z*x%Mo;
	return z;
}

void inc(int &x,int y) {x=x+y>=Mo?x+y-Mo:x+y;}
void dec(int &x,int y) {x=x-y<0?x-y+Mo:x-y;}
void FWT(int *a,int len) {for(int i=1;i<len;i<<=1) for(int j=0;j<len;j+=i<<1) for(int k=0;k<i;k++) inc(a[j+k+i],a[j+k]);}
void IFWT(int *a,int len) {for(int i=1;i<len;i<<=1) for(int j=0;j<len;j+=i<<1) for(int k=0;k<i;k++) dec(a[j+k+i],a[j+k]);}

ll h[20],p[20];

void get_ln() {
	p[0]=0;
	fo(i,1,n) {
		p[i]=h[i]*i;
		fo(j,1,i-1) p[i]-=p[j]*h[i-j]%Mo*j;
		p[i]=p[i]%Mo*inv[i]%Mo;
	}
	fo(i,0,n) h[i]=(p[i]+Mo)%Mo;
}

void get_exp() {
	p[0]=1;
	fo(i,1,n) {
		p[i]=0;
		fo(j,1,i) p[i]+=h[j]*p[i-j]%Mo*j;
		p[i]=p[i]%Mo*inv[i]%Mo;
	}
	fo(i,0,n) h[i]=p[i];
}

int main() {
	scanf("%d%d%d",&L,&k,&Mo);
	int all=pwr(k,L);
	if (L&1) {
		//puts("0");
		printf("%d\n",all);
		return 0;
	}
	fac[0]=1;fo(i,1,L) fac[i]=(ll)fac[i-1]*i%Mo;
	inv[L]=pwr(fac[L],Mo-2);fd(i,L-1,0) inv[i]=(ll)inv[i+1]*(i+1)%Mo;
	fo(s,1,L) cnt[s]=cnt[s^(s&-s)]+1;
	fo(i,0,18) if (L>>i&1) bit[++n]=1<<i;
	len=1<<n;
	fo(i,0,len-1) {
		int s=0;
		fo(j,1,n) if (i>>(j-1)&1) s+=bit[j];
		g[cnt[i]][i]=inv[s];
	}
	fo(i,2,n) inv[i]=(ll)(Mo-Mo/i)*inv[Mo%i]%Mo;
	fo(i,0,n) FWT(g[i],len);
	fo(s,0,len-1) {
		fo(i,0,n) h[i]=g[i][s];
		get_ln();
		fo(i,0,n) h[i]=h[i]*k;
		get_exp();
		fo(i,0,n) f[i][s]=h[i];
	}
	IFWT(f[n],len);
	int ans=(ll)f[n][len-1]*fac[L]%Mo;
	ans=(all-ans+Mo)%Mo;
	printf("%d\n",ans);
	return 0;
}


#include"bits/stdc++.h"
#define F(i,j,n) for(register int i=j;i<=n;i++)
#define D(i,j,n) for(register int i=j;i>=n;i--)
#define ll unsigned long long
#define N 250010
using namespace std;
namespace io{
	const int L=(1<<19)+1;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,st[55];int f,tp;
	char gc(){
		if(iS==iT){
			iT=(iS=ibuf)+fread(ibuf,1,L,stdin);
			return iS==iT?EOF:*iS++;
		}
		return*iS++;
	}
	template<class I>void gi(I&x){
		for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
};
using io::gi;
using io::gc;
int n,k,p,l,b[N];
ll x[26][N],f[N],g[N],a,d;
ll pw(ll x,int y=p-2){ll a=1;while(y){if(y&1)a=a*x%p;x=x*x%p;y>>=1;}return a;}
ll dfs(int k,int n){
	if(b[n]<k)return 0;
	if(k==1)return x[k][n]=g[n];
	if(x[k][n])return x[k][n];
	int t=0;
	for(int i=n&(n-1);i;i=n&(i-1))if(b[i]>=k-1){
		x[k][n]+=dfs(k-1,i)*g[n^i];
		if(++t==18)t=0,x[k][n]%=p;
	}
	x[k][n]%=p;
//	printf("%d %d %lld\n",k,n,x[k][n]);
	return x[k][n];
}
ll c(int n,int m){return f[n]*g[m]%p*g[n-m]%p;}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("255x.in","r",stdin);
	freopen("255x.out","w",stdout);
	#endif
	gi(n);gi(k);gi(p);
	F(i,1,n)b[i]=b[i>>1]+(i&1);
	a=pw(k,n);
	if(n&1){printf("%d\n",a);return 0;}
	l=min(k,b[n]);
	f[0]=1;F(i,1,n)f[i]=f[i-1]*i%p;
	g[n]=pw(f[n]);D(i,n,1)g[i-1]=g[i]*i%p;
	D(i,l,1)d+=c(k,i)*dfs(i,n);
//	cout<<a<<endl<<d<<endl;
	a=(a-d%p*f[n]%p+p)%p;
	printf("%lld\n",a);
//	printf("%.3lf\n",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}