#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <string>
#include <list>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

//const LL mod=1e9+7;
const double PI=acos(-1);
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
inline int readint(){int sum=0;char c=getchar();bool f=0;while(c<'0'||c>'9'){if(c=='-') f=1;c=getchar();}while(c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}if(f) return -sum;return sum;}
inline LL readLL(){LL sum=0;char c=getchar();bool f=0;while(c<'0'||c>'9'){if(c=='-') f=1;c=getchar();}while(c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}if(f) return -sum;return sum;}

const int maxn=250015;
LL inv[maxn],p[maxn];
void CNumberInit(int MAXNUM,LL mod){
    inv[0]=inv[1]=1;
    p[0]=p[1]=1;
    for(int i=2;i<=MAXNUM;i++){
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
        p[i]=p[i-1]*i%mod;
    }
    for(int i=2;i<=MAXNUM;i++) inv[i]=inv[i-1]*inv[i]%mod;
}

LL qmod(LL a,LL b,LL c){
    LL ret=1;
    for(;b;b>>=1){
        if(b&1) ret=ret*a%c;
        a=a*a%c;
    }
    return ret;
}

vector<int> megumi;
LL mod;
int k;
LL dp[20][250005];

int main(){ios_base::sync_with_stdio(0);cin.tie(0);
    LL n;cin>>n>>k>>mod;
    CNumberInit(n+5,mod);
    if(n%2){
        cout<<qmod(k,n,mod)<<endl;
    }else{
        for(int i=2;i<=n;i+=2){
            if((i&n)==i) megumi.push_back(i);
        }
        LL ans=0;
        dp[0][n]=1;
        for(int i=1;i<=min(17,k);i++){
            int tmpans=0;
            for(int j=0;j<megumi.size();j++){
                if(!dp[i-1][megumi[j]]) continue;
                int Megumi=megumi[j]&(megumi[j]-1);
                int step=Megumi^megumi[j];
                for(int k=step;k<=Megumi;k+=step){
                    if((k&Megumi)==k){
                        dp[i][k]+=dp[i-1][megumi[j]]*p[megumi[j]]%mod
                            *inv[k]%mod*inv[megumi[j]-k]%mod;
                        dp[i][k]%=mod;
                    }
                }
                tmpans=(tmpans+dp[i-1][megumi[j]])%mod;
            }
            ans+=tmpans*inv[k-i]%mod;
        }
        ans=ans*p[k]%mod;
        ans=qmod(k,n,mod)-ans;
        ans=(ans%mod+mod)%mod;
        cout<<ans<<endl;
    }
    return 0;
}
//1000000007
//cout.setf(ios::fixed);
//cout<<fixed<<setprecision(10)<<s<<endl;


#include <bits/stdc++.h>
using namespace std;
const int N = 250005;
int n,kk,P;
using ll = long long;
ll qpow(ll a,ll b)
{
    ll ret = 1;
    while (b)
    {
        if (b & 1) ret = ret * a % P;
        a = a * a % P;b >>= 1;
    }
    return ret;
}
inline int lowbit(int x) {return x & -x;}
ll fac[N],inv[N],dp[26][N];
ll dfs(int cur,int msk)
{
    if (dp[cur][msk] != -1) return dp[cur][msk];
    if (!msk)
    {
        dp[cur][msk] = fac[n];
        for (int i = 1;i <= cur;i++)
            dp[cur][msk] = dp[cur][msk] * (kk - i + 1) % P;
        return dp[cur][msk];
    }
    dp[cur][msk] = 0;int tmp = msk - lowbit(msk);
    for (int i = tmp;i > 0;i = (i - 1) & tmp)
        dp[cur][msk] = (dp[cur][msk] + inv[msk - i] * dfs(cur + 1,i)) % P;
    dp[cur][msk] = (dp[cur][msk] + inv[msk] * dfs(cur + 1,0)) % P;
    return dp[cur][msk];
}
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n >> kk >> P;ll ans = qpow(kk,n);
    if (n & 1) {cout << ans << endl;return 0;}
    fac[0] = 1;
    for (int i = 1;i <= n;i++) fac[i] = fac[i - 1] * i % P;
    inv[n] = qpow(fac[n],P - 2);
    for (int i = n;i > 0;--i) inv[i - 1] = inv[i] * i % P;
    memset(dp,-1,sizeof(dp));ans = (ans - dfs(0,n) + P) % P;
    cout << ans << endl;
    return 0;
}
// 非卡常做法需要用FWT
// 卡常做法: 每次强制将Ai规定为含有msk的lowbit的那一个，最后当msk=0时用排列乘回来


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
const int N=250100;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
int mod,m;
int siz[N],inv[N];
struct DT{ int a[19]; }f[N];
inline int qpow(int x,int y) {
	int ans=1;
	while (y) {
		if (y&1) ans=1LL*ans*x%mod;
		x=1LL*x*x%mod;y>>=1;
	}
	return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("CF838C.in","r",stdin);
	freopen("CF838C.out","w",stdout);
#endif
	int i,s,n=gi(),k=gi(),a,b,y,t[20]={},p[20]={};DT *F,*G;mod=gi();
	if (n&1) return printf("%d\n",qpow(k,n)),0;
	for (i=0;i<18;i++) m+=n>>i&1;
	for (i=0,s=1;i<=n;i++) {
		inv[i]=i<2?1:-1LL*(mod/i)*inv[mod%i]%mod;
		siz[i]=siz[i>>1]+(i&1);
		s=1LL*s*inv[i]%mod;
		if ((i&n)==i)
			f[i].a[siz[i]]=s;
	}
	for (i=0;i<18;i++)
		if (n>>i&1)
			for (s=n;s;(--s)&=n)
				if (s>>i&1)
					for (a=0,F=&f[s^(1<<i)],G=&f[s];a<=m;a++) (G->a[a]+=F->a[a])%=mod;
	for (i=0;i<=n;i++) if ((i&n)==i) {
			F=&f[i];
			for (a=0;a<=m;a++) t[a]=F->a[a],F->a[a]=!a;
			for (y=k;y;) {
				if (y&1) {
					for (a=0;a<=m;a++) p[a]=F->a[a],F->a[a]=0;
					for (a=0;a<=m;a++)
						if (t[a])
							for (b=0;a+b<=m;b++)
								F->a[a+b]=(F->a[a+b]+1LL*t[a]*p[b])%mod;
				}
				if (y>>=1) {
					for (a=0;a<=m;a++) p[a]=t[a],t[a]=0;
					for (a=0;a<=m;a++)
						if (p[a])
							for (b=0;a+b<=m;b++)
								t[a+b]=(t[a+b]+1LL*p[a]*p[b])%mod;
				}
			}
		}
	for (i=0;i<18;i++)
		if (n>>i&1)
			for (s=n;s;(--s)&=n)
				if (s>>i&1)
					for (a=0,F=&f[s^(1<<i)],G=&f[s];a<=m;a++) (G->a[a]-=F->a[a])%=mod;
	s=f[n].a[m];
	for (i=n;i;i--) s=1LL*s*i%mod;
	printf("%d\n",((qpow(k,n)-s)%mod+mod)%mod);
	return 0;
}


#include <bits/stdc++.h>
#define inf 17446744073709551616ull
#define ll unsigned long long
using namespace std;
ll mod,n,k,dp[19][300005];
ll fac[300005],inv[300005];
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;
ll ti=max(n,k);
for (i=1;i<=ti;i++) fac[i]=fac[i-1]*((ll)(i))%mod;
inv[ti]=qpow(fac[ti],mod-2ll);
for (i=ti-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(18,((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>j;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)
{if (dp[i][j]>=mod) dp[i][j]%=mod;
}
ll res=dp[i][n]*cal(k,i);res%=mod;
res=res*fac[i]%mod;
ans=(ans+res>=mod?ans+res-mod:ans+res);
}
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<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define HEAP(T) priority_queue<T>
#define heap(T) priority_queue<T,vector<T>,greater<T> >
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define error(...) fprintf(stderr,__VA_ARGS__)
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define inf 0x3f3f3f3f
#define clr(a,x) memset(a,x,sizeof(a))
#define cpy(a,x) memcpy(a,x,sizeof(a))
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
using namespace std;

const int N=300005,M=100005;
int mod=1e9+7;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
template<class T,class S> inline void ch(T &x,const S y){x=(x+y)%mod;}
const int BufferSize=1<<16;
char buffer[BufferSize],*Bufferhead,*Buffertail;
bool Terminal;
inline char Getchar(){
	if(Bufferhead==Buffertail){
		int l=fread(buffer,1,BufferSize,stdin);
		if(!l){Terminal=1;return 0;}
		Buffertail=(Bufferhead=buffer)+l;
	}
	return *Bufferhead++;
}
template<class T>inline bool read(T &x){
	x=0;char c=Getchar(),rev=0;
	while(c<'0'||c>'9'){c=Getchar();rev|=c=='-';if(Terminal)return 0;}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=Getchar();
	x=rev?-x:x;
	return 1;
}
inline bool reads(char *x){
	char c=Getchar();
	while(c<33||c>126){c=Getchar();if(Terminal)return 0;}
	while(c>=33&&c<=126) (*x++)=c,c=Getchar();
	*x=0;return 1;
}
template<class T>inline void print(T x,const char c){
	if(!x){putchar('0');putchar(c);return;}
	if(x<0) putchar('-'),x=-x;
	int m=0,a[10];
	while(x) a[m++]=x%10,x/=10;
	while(m--) putchar(a[m]+'0');
	putchar(c);
}
inline int exp(int x,int y,const int mod=::mod){
	int ans=1;
	while(y){
		if(y&1) ans=(ll)ans*x%mod;
		x=(ll)x*x%mod;y>>=1;
	}return ans;
}

int n,k,fac[N],ifac[N],dp[2][N];
inline int C(int x,int y){return (ll)fac[x]*ifac[x-y]%mod*ifac[y]%mod;}

int main(){
#ifdef rqgao2014
	freopen("input.txt","r",stdin);
#endif
	scanf("%d%d%d",&n,&k,&mod);
	ifac[0]=fac[0]=1;
	for(int i=1;i<=n;i++) fac[i]=(ll)fac[i-1]*i%mod;
	ifac[n]=exp(fac[n],mod-2);
	for(int i=n-1;i;i--) ifac[i]=(ll)ifac[i+1]*(i+1)%mod;
	if(n&1){
		printf("%d\n",exp(k,n));
		return 0;
	}
	int ans=0;
	dp[0][n]=1;
	for(int i=1;i<=k;i++){
		clr(dp[i&1],0);
		int res=0;
		for(int j=1;j<=n;j++){
			int tmp=dp[i-1&1][j];
			if(!tmp) continue;
			int u=j^(j&-j);
			for(int k=u;k;k=(k-1)&u)
				ch(dp[i&1][k],(ll)tmp*C(j,k));
			ch(res,tmp);
		}
		ch(ans,(ll)res*ifac[k-i]);
	}
	ans=(ll)ans*fac[k]%mod;
	ans=(exp(k,n)-ans+mod)%mod;
	printf("%d\n",ans);
	return 0;
}


/*
@Date    : 2020-10-24 08:06:07
@Author  : Adscn (adscn@qq.com)
@Link    : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
template<typename T>IL bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
template<typename T>IL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>
IL T geti()
{
	RG T xi=0;
	RG char ch=gc;
	bool f=0;
	while(!isdigit(ch))ch=='-'?f=1:f,ch=gc;
	while(isdigit(ch))xi=xi*10+ch-48,ch=gc;
	return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
	if(k<0)k=-k,putchar('-');
	if(k>=10)pi(k/10);
	putchar(k%10+'0');
	if(ch)putchar(ch);
}
const int N=3e5+10;
int P;
int f[19][N];
int ifac[N],fac[N],inv[N];
inline ll fpow(ll a,int b,ll c=1){
	for(;b;b>>=1,a=a*a%P)if(b&1)c=c*a%P;
	return c;
}
inline void init(int n){
	fac[0]=1;
	for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%P;
	ifac[n]=fpow(fac[n],P-2);
	for(int i=n-1;~i;--i)ifac[i]=ifac[i+1]*(i+1ll)%P;
	inv[1]=1;
	for(int i=2;i<=n;++i)inv[i]=1ll*(P-P/i)*inv[P%i]%P;
}
inline void add(int &x,int y){x+=y;if(x>=P)x-=P;}
inline void sub(int &x,int y){x-=y;x-=(x>>31)*P;}
inline void FMT(int *f,int n)
{
	for(int l=1;l<n;l<<=1)
		for(int i=0;i<n;i+=l<<1)
			for(RG int j=0;j<l;++j)
				add(f[i+j+l],f[i+j]);
}
inline void IFMT(int *f,int n)
{
	for(int l=1;l<n;l<<=1)
		for(int i=0;i<n;i+=l<<1)
			for(RG int j=0;j<l;++j)
				sub(f[i+j+l],f[i+j]);
}
int n,k;
#define pop(x) __builtin_popcount(x)
int lp[N];
inline void ln(int *f,int *g,int n){
	g[0]=0;
	for(int i=1;i<n;++i)
	{
		g[i]=0;
		for(int j=1;j<i;++j)g[i]=(g[i]+1ll*f[i-j]*g[j]%P*j)%P;
		g[i]=(f[i]+1ll*g[i]*(P-inv[i]))%P;
	}
}
inline void exp(int *f,int *g,int n){
	g[0]=1;
	for(int i=1;i<n;++i){
		g[i]=0;
		for(int j=1;j<=i;++j)g[i]=(g[i]+1ll*g[i-j]*f[j]%P*j)%P;
		g[i]=(1ll*g[i]*inv[i])%P;
	}
}
int main(void)
{
	#ifndef ONLINE_JUDGE
// 	File("onmyodo");
	#endif
	n=gi,k=gi,P=gi;
	int ans=fpow(k,n);
	if(~n&1){
		int m=(int)log2(n)+1;
		init(1<<m);
		for(int i=0;i<(1<<m);++i)f[pop(i)][i]=ifac[i];
		for(int i=0;i<=m;++i)FMT(f[i],1<<m);
		static int F[20],G[20];
		for(int i=0;i<(1<<m);++i){
			for(int j=0;j<=m;++j)F[j]=f[j][i];
			ln(F,G,m+1);
			for(int j=0;j<=m;++j)G[j]=1ll*G[j]*k%P;
			exp(G,F,m+1);
			for(int j=0;j<=m;++j)f[j][i]=F[j];
		}
		IFMT(f[pop(n)],1<<m);
		ans=(P+ans-1ll*f[pop(n)][n]*fac[n]%P)%P;
	}
	pi(ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using db = double;
#define pb push_back
#define fir first
#define sec second

int n, k, mod;
int f[250006][27];

#define lbit(i) ((i)&-(i))

const int N = 250001;
int fac[N], inv[N], ifac[N];

int main() {

	cin >> n >> k >> mod;

	inv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
	for (int i = 2; i < N; ++i) {
		inv[i] = (ll)mod / i * (mod - inv[mod % i]) % mod;
		fac[i] = (ll)fac[i - 1] * i % mod;
		ifac[i] = (ll)ifac[i - 1] * inv[i] % mod;
	}

	ll ans = 1;
	for (int i = 1; i <= n; ++i)
		(ans *= k) %= mod;

	if (n % 2 == 0) {
		f[0][0] = 1;
		for (int i = 1; i <= n; ++i) if ((n & i) == i) // 要从小到大枚举子集
			for (int msk = i - lbit(i), j = msk; ; --j &= msk) {
				int len = min(k - 1, __builtin_popcount(j));
				for (int t = 0; t <= len; ++t)
					f[i][t + 1] = (f[i][t + 1] + (ll)f[j][t] * ifac[i - j]) % mod;
				if (!j) break;
			}
		for (int i = 1; i <= k; ++i)
			(ans -= (ll)f[n][i] * fac[n] % mod * fac[k] % mod * ifac[k - i]) %= mod;
	}

	cout << (ans + mod) % mod << endl;

	return 0;
}


#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;
	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];
	}
	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){
		for(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);
		for(int a=0;a<=n;a++)for(int j=0;j<m;j++)
			if(dp[a][j])for(int l=a;l<=n;l++)tmp[l][j]+=dp[a][j]*dp[l-a][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){
			for(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);
			for(int a=0;a<=n;a++)for(int j=0;j<m;j++)
				if(dp[a][j])for(int l=a;l<=n;l++)tmp[l][j]+=dp[a][j]*suma[l-a][j];
			for(int t=0;t<=n;t++)for(int j=0;j<m;j++)dp[t][j]=tmp[t][j]%P;
		}
	}
	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;
}


// In the name of God

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef unsigned int ui;

const int Lg=18,Tw=(1<<Lg),Max=Tw+10;

int Ans[Max][Lg+3],tmp[Lg+3],tmp2[Lg+3],fac[Max],ifac[Max],N,n,mod=1e9+7;

int Pow(int a,int b){
    int ans=1;
    while(b){
	if(b&1) ans=1ll*ans*a %mod;
	a=1ll*a*a %mod;
	b>>=1;
    }
    return ans;
}
inline void Add(int *a,int *b){
    for(int i=0;i<=N;i++)
	a[i]=(a[i]+b[i])%mod;
}
inline void Sub(int *a,int *b){
    for(int i=0;i<=N;i++)
	a[i]=(a[i]-b[i])%mod;
}
inline void Mul(int *ans,int *a,int *b){
    memset(tmp,0,sizeof tmp);
    for(int i=0;i<=N;i++){
	for(int j=0;j<=i;j++){
	    tmp[i]=(1ll*tmp[i]+1ll*a[j]*b[i-j])%mod;
	}
    }
    memcpy(ans,tmp,sizeof tmp);
}
void trans1(int a[][Lg+3]){
    for(int i=0;i<N;i++)
	for(int msk=n;msk>=0;msk--)
	    if(bit(msk,i))
		Add(a[msk],a[msk ^ (1<<i)]);
}
void trans2(int a[][Lg+3]){
    for(int i=0;i<N;i++)
	for(int msk=n;msk>=0;msk--)
	    if(bit(msk,i))
		Sub(a[msk],a[msk ^ (1<<i)]);
}

vector<int>vec;

int main(){
    int K;cin>>n>>K>>mod;
    int ANS=Pow(K,n);
    if(n&1) return cout<<ANS<<endl,0;
    N=__builtin_popcount(n);
    int Nbef=n;
    for(int i=0;i<Lg;i++){
	if(bit(n,i))
	    vec.PB(i);
    }
    n=(1<<N)-1;
    fac[0]=1;
    for(int i=1;i<Max;i++)
	fac[i]=1ll*i*fac[i-1]%mod;
    ifac[Max-1]=Pow(fac[Max-1],mod-2);
    for(int i=Max-2;i>=0;i--)
	ifac[i]=1ll*(i+1)*ifac[i+1] %mod;
    for(int i=0;i<=n;i++){
	int Real=0;
	for(int j=0;j<N;j++)
	    Real+=bit(i,j)*(1<<vec[j]);
	Ans[i][__builtin_popcount(i)]=ifac[Real];
    }
    trans1(Ans);
    for(int i=0;i<=n;i++){
	int KK=K;
	memset(tmp2,0,sizeof tmp2); tmp2[0]=1;

	while(KK){
	    if(KK&1) Mul(tmp2,tmp2,Ans[i]);
	    Mul(Ans[i],Ans[i],Ans[i]);
	    KK>>=1;
	}
	memcpy(Ans[i],tmp2,sizeof tmp2);
    }
    trans2(Ans);
    ANS=(1ll*ANS-1ll*Ans[n][N]*fac[Nbef])%mod;
    if(ANS<0) ANS+=mod;
    return cout<<ANS<<endl,0;
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;

const int N = 1e6+10;
int n, k, P, fac[N], ifac[N];
int f[N], g[N];
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}

void add(int &a, ll b) {a=(a+b)%P;}

int main() {
	scanf("%d%d%d",&n,&k,&P);
	fac[0] = 1;
	REP(i,1,N-1) fac[i]=(ll)fac[i-1]*i%P;
	ifac[N-1] = inv(fac[N-1]);
	PER(i,0,N-2) ifac[i]=(ll)ifac[i+1]*(i+1)%P;
	int ans = qpow(k,n);
	if (n%2==0) {
		f[n] = fac[n];
		REP(z,1,k) {
			REP(i,0,n) g[i] = 0;
			for (int i=n; i; i=(i-1)&n) if (f[i]) {
				for (int j=i; 2*j>=i; j=(j-1)&i) {
					add(g[i^j],(ll)f[i]*ifac[j]);
				}
			}
			REP(i,0,n) swap(f[i],g[i]);
			int res = f[0];
			REP(j,0,z-1) res = (ll)res*(k-j)%P;
			ans = (ans-res)%P;
		}
	}
	if (ans<0) ans += P;
	printf("%d\n", ans);
}


/*
@Date    : 2020-10-24 08:06:07
@Author  : Adscn (adscn@qq.com)
@Link    : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
template<typename T>IL bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
template<typename T>IL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>
IL T geti()
{
	RG T xi=0;
	RG char ch=gc;
	bool f=0;
	while(!isdigit(ch))ch=='-'?f=1:f,ch=gc;
	while(isdigit(ch))xi=xi*10+ch-48,ch=gc;
	return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
	if(k<0)k=-k,putchar('-');
	if(k>=10)pi(k/10);
	putchar(k%10+'0');
	if(ch)putchar(ch);
}
const int N=3e5+10;
int P=1e9+9;
int f[19][N];
int ifac[N],fac[N],inv[N];
inline ll fpow(ll a,int b,ll c=1){
	for(;b;b>>=1,a=a*a%P)if(b&1)c=c*a%P;
	return c;
}
inline void init(int n){
	fac[0]=1;
	for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%P;
	ifac[n]=fpow(fac[n],P-2);
	for(int i=n-1;~i;--i)ifac[i]=ifac[i+1]*(i+1ll)%P;
	inv[1]=1;
	for(int i=2;i<=n;++i)inv[i]=1ll*(P-P/i)*inv[P%i]%P;
}
inline void add(int &x,int y){x+=y;if(x>=P)x-=P;}
inline void sub(int &x,int y){x-=y;x-=(x>>31)*P;}
inline int sub(int x){return x-(x>>31)*P;}
int t[20],g[20];
inline void fpow(int *f,int n,int k){
	memcpy(t,f,4*n+4);
	f[0]=1-f[0],g[0]=0;
	for(int i=1;i<n;++i)
	{
		f[i]=g[i]=0;
		for(int j=1;j<i;++j)
			f[i]=(f[i]+1ll*f[i-j]*g[j])%P,
			g[i]=(g[i]+1ll*t[i-j]*g[j])%P;
		sub(f[i],g[i]);
		f[i]=(t[i]+1ll*f[i]*inv[i])%P*k%P,
		g[i]=(t[i]-1ll*g[i]*inv[i])%P*i%P;
	}
}
inline void FMT(int *f,int n)
{
	for(int l=1;l<n;l<<=1)
		for(int i=0;i<n;i+=l<<1)
			for(RG int j=0;j<l;++j)
				add(f[i+j+l],f[i+j]);
}
inline void IFMT(int *f,int n)
{
	for(int l=1;l<n;l<<=1)
		for(int i=0;i<n;i+=l<<1)
			for(RG int j=0;j<l;++j)
				sub(f[i+j+l],f[i+j]);
}
int n,k;
#define pop(x) __builtin_popcount(x)
int main(void)
{
	n=gi,k=gi;P=gi;
	int ans=fpow(k,n);
	if(~n&1){
		int m=(int)log2(n)+1;
		init(1<<m);
		for(int i=0;i<(1<<m);++i)f[pop(i)][i]=ifac[i];
		for(int i=0;i<=m;++i)FMT(f[i],1<<m);
		static int F[20];
		for(int i=0;i<=n;++i){
			for(int j=0;j<=m;++j)F[j]=f[j][i];
			fpow(F,m+1,k);
			for(int j=0;j<=m;++j)f[j][i]=F[j];
		}
		IFMT(f[pop(n)],1<<m);
		ans=(P+ans-1ll*f[pop(n)][n]*fac[n]%P)%P;
	}
	pi(ans);
	return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using ll = long long;

#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
namespace io {
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}
  inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}
  inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}
  template<class T>
  inline void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while(isspace(ch = getc()));
    if(ch == '-') ch = getc(), f = -1;
    do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));
    x *= f;
  }
  template<class T, class ...Args>
  inline void read(T &x, Args&... args) {read(x); read(args...);}
  template<class T>
  inline void write(T x) {
    static char stk[128];
    int top = 0;
    if(x == 0) {putc('0'); return;}
    if(x < 0) putc('-'), x = -x;
    while(x) stk[top++] = x % 10, x /= 10;
    while(top) putc(stk[--top] + '0');
  }
  template<class T, class ...Args>
  inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}
  inline void space() {putc(' ');}
  inline void endl() {putc('\n');}
  struct _flush {~_flush() {flush();}} __flush;
};
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

int M;
inline int add(int x, int y) { return x + y >= M ? x + y - M : x + y; }
template <class... Args> inline int add(int x, int y, Args... args) { return add(add(x, y), args...); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + M : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % M; }
template <class... Args> inline int mul(int x, int y, Args... args) { return mul(mul(x, y), args...); }
inline void inc(int &x, int y=1) { x += y; if(x >= M) x -= M; }
inline void dec(int &x, int y=1) { x -= y; if(x < 0) x += M; }
inline int power(int x, int y) {
  int res = 1;
  for(; y; y>>=1, x = mul(x, x)) if(y & 1) res = mul(res, x);
  return res;
}
inline int inv(int x) { return power(x, M - 2); }

const int N = 250005, K = 19;
int f[N][K];
int n, k;

int fac[N], ifac[N];
void prefac(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = mul(fac[i - 1], i);
  ifac[n] = inv(fac[n]);
  for (int i = n - 1; i >= 0; --i) ifac[i] = mul(ifac[i + 1], i + 1);
}

int dp(int s, int c) {
  if (!s) return 1;
  if (c == k) return 0;
  int &res = f[s][c];
  if (res != -1) return res;
  res = 0;
  int hb = 1;
  while (hb <= s) hb <<= 1;
  hb >>= 1;
  for (int t = s; t != -1; --t) {
    t &= s;
    if (!(t & hb)) return res;
    inc(res, mul(ifac[t], k - c, dp(s ^ t, c + 1)));
  }
  return res;
}

int main() {
  memset(f, -1, sizeof(f));
  read(n, k, M);
  prefac(n);
  if (n & 1) {
    write(power(k, n)), endl();
    return 0;
  }
  write(sub(power(k, n), mul(fac[n], dp(n, 0)))), endl();
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,k,p;
int fpow(int a,int b){
	int ans=1,t=a;
	while(b){
		if(b&1)ans=1ll*ans*t%p;
		t=1ll*t*t%p;b>>=1;
	}
	return ans;
}
int lowbit(int x){
	return x&(-x);
}
int f[30][250005];
int fac[250005],inv[250005];
void init()
{
	fac[0]=1;
	for(int i=1;i<=n;i++)
		fac[i]=1ll*fac[i-1]*i%p;
	inv[n]=fpow(fac[n],p-2);
	for(int i=n;i>=1;i--)
		inv[i-1]=1ll*inv[i]*i%p;
	//return ;
}
int solve(int cur,int S){
	if(f[cur][S]!=-1)
		return f[cur][S];
	if(!S){
		f[cur][S]=fac[n];
		for(int i=1;i<=cur;i++)
			f[cur][S]=1ll*f[cur][S]*(k-i+1)%p;
		return f[cur][S];
	}
	f[cur][S]=0;
	int U=S-lowbit(S);
	for(int T=U;T;T=(T-1)&U)
		f[cur][S]=(f[cur][S]+1ll*inv[S-T]*solve(cur+1,T))%p;
	f[cur][S]=(f[cur][S]+1ll*inv[S]*solve(cur+1,0))%p;
	return f[cur][S];
}
int main(){
	scanf("%d %d %d",&n,&k,&p);
	int ans=1;
	for(int i=1;i<=n;i++)
		ans=1ll*ans*k%p;
	if(n%2==0){
		init();
		memset(f,-1,sizeof(f));
		ans=(ans-solve(0,n)+p)%p;
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <cassert>
#include <unordered_map>
#include <fstream>
#include <random>
#include <cstring>
#include <complex>
#include <bitset>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef double ld;

// map<vector<int>, bool> mp;

// bool f(const vector<int> &a) {
//     int l = 0;
//     int x = 1;
//     for (int i = 1; i <= sz(a); ++i) {
//         if (i < sz(a) && a[i] == a[i - 1]) {
//             ++x;
//         } else {
//             x >>= 1;
//             for (; x > 0; x >>= 1)
//                 l -= x;
//             x = 1;
//         }
//     }
//     int sum = sz(a);
//     sum >>= 1;
//     for (; sum > 0; sum >>= 1)
//         l += sum;
//     return l > 0;
// }

// bool solve(vector<int> a) {
//     if (mp.count(a))  {
//         return mp[a];
//     }
//     for (int i = 0; i < sz(a); ++i) {
//         if (i == sz(a) - 1 || a[i] != a[i + 1]) {
//             vector<int> c = a;
//             c.erase(c.begin() + i);
//             if (!solve(c)) {
//                 return mp[a] = 1;
//             }
//         }
//     }
//     return mp[a] = (f(a) > 0 ? 0 : 1);
// }

// void solve(int n, int lim, vector<int> &a) {
//     if (n == 0) {
//         solve(a);
//         return;
//     }
//     // for (int i = 0; i < sz(a); ++i)
//     //     cout << a[i] << " ";
//     // cout << "\n";

//     if (n >= lim) {
//         a.push_back(lim);
//         solve(n - 1, lim, a);
//         a.pop_back();
//     }
//     if (lim > 1) {
//         a.push_back(lim - 1);
//         solve(n - 1, lim - 1, a);
//         a.pop_back();
//     }
//     if (sz(a) == 0 && lim > 1) {
//         solve(n, lim - 1, a);
//     }
// }

ll binPow(ll a, int b, int p) {
    if (b == 0)
        return 1;
    ll x = binPow(a, b / 2, p);
    x *= x;
    x %= p;
    if (b & 1) {
        x *= a;
        x %= p;
    }
    return x;
}

int n, k, p;
const int MAXN = 250250;
ll fact[MAXN], rfact[MAXN];
ll pw[MAXN];
int cnt = 0;

ll g[MAXN][17];

void solve(int n, const vector<int> &bit, int k, int l) {
    if (k == sz(bit)) {
        ++cnt;
        for (int i = 1; i < 17; ++i) {
            g[n][i] += rfact[l] * g[n ^ l][i - 1];
            if (cnt >= 8) {
                g[n][i] %= p;
            }
        }
        if (cnt >= 8)
            cnt = 0;
        return;
    }
    solve(n, bit, k + 1, l);
    solve(n, bit, k + 1, l ^ (1 << bit[k]));
}

int main() {
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // int n;
    // cin >> n;
    // vector<int> a;
    // mp[a] = 0;
    // solve(n, n, a);

    // for (auto a : mp) {
    //     if (sz(a.first) < n)
    //         continue;
    //     cout << a.second << " ---> ";
    //     for (int i = 0; i < sz(a.first); ++i)
    //         cout << a.first[i] << " ";
    //     cout << "--- " << f(a.first);
    //     cout << "\n";
    // }

    cin >> n >> k >> p;
    if (n & 1) {
        cout << binPow(k, n, p) << "\n";
        return 0;
    }

    fact[0] = 1;
    rfact[0] = 1;
    for (int i = 1; i <= n; ++i) {
        fact[i] = fact[i - 1] * i % p;
        rfact[i] = binPow(fact[i], p - 2, p);
    }

    g[0][0] = 1;
    for (int i = 1; i <= n; ++i) {
        if ((i & n) != i)
            continue;
        vector<int> bit;
        for (int j = 0; j < 18; ++j) {
            if ((1 << j) & i)
                bit.push_back(j);
        }
        solve(i, bit, 1, 1 << bit[0]);
        for (int j = 0; j < 17; ++j)
            g[i][j] %= p;
    }
    ll ans = 0;
    for (int i = 1; i <= min(16, k); ++i) {
        g[n][i] %= p;
        ll cur = fact[n] * g[n][i];
        cur %= p;
        cur *= fact[k];
        cur %= p;
        cur *= rfact[k - i];
        cur %= p;
        ans += cur;
        ans %= p;
    }
    ans = binPow(k, n, p) - ans;
    if (ans < 0)
        ans += p;
    cout << ans << "\n";
    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<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,mod;
int fac[250003],inv[250003];
int dp[27][250003];
int lowbit(int x){return x&(-x);}
int fpow(int x,int y)
{
	int res=1;
	while(y)
	{
		if(y&1) res=1ll*res*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return res;
}
void init()
{
	fac[0]=1;
	for(int i=1;i<=n;i++)
		fac[i]=1ll*fac[i-1]*i%mod;
	inv[n]=fpow(fac[n],mod-2);
	for(int i=n-1;i>=0;i--)
		inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
int dfs(int x,int s)
{
	if(dp[x][s]!=-1) return dp[x][s];
	if(s==0) return dp[x][s]=1ll*fac[n]*fac[k]%mod*inv[k-x]%mod;
	int now=s-lowbit(s);
	dp[x][s]=0;
	for(int t=now;t;t=(t-1)&now)
		dp[x][s]=(dp[x][s]+1ll*dfs(x+1,t)*inv[s-t]%mod)%mod;
	dp[x][s]=(dp[x][s]+1ll*dfs(x+1,0)*inv[s]%mod)%mod;
	return dp[x][s];
}
int main()
{
	scanf("%d%d%d",&n,&k,&mod);
	init();
	int ans=fpow(k,n);
	if(n%2==0)
	{
		memset(dp,-1,sizeof(dp));
		ans=(ans-dfs(0,n)+mod)%mod;
	}
	printf("%d",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int MOD;
int mpow(int x, int n) {
	int res = 1;
	while (n) {
		if (n & 1) res = 1LL * res * x % MOD;
		x = 1LL * x * x % MOD;
		n >>= 1;
	}
	return res;
}
const int N = 3e5 + 5;
unsigned long long dp[27][N];
int rf[N], f[N];
int permute(int n, int m) {
	return 1LL * f[n] * rf[n - m] % MOD;
}
int main() {
	int n, K;
	scanf("%d%d%d", &n, &K, &MOD);
	if (n & 1) {
		printf("%d\n", mpow(K, n));
		return 0;
	}
	f[0] = 1;
	for (int i = 1; i <= n; ++ i) f[i] = 1LL * f[i - 1] * i % MOD;
	rf[n] = mpow(f[n], MOD - 2);
	for (int i = n - 1; i >= 0; -- i) rf[i] = 1LL * rf[i + 1] * (i + 1) % MOD;
	dp[0][0] = 1;
	for (int k = 0; k < K; ++ k) {
		for (int i = 1; i <= n; ++ i) {
			if ((i & n) != i) continue;
			int cnt = 0;
			unsigned long long &res = dp[k + 1][i];
			int hb = (1 << (31 - __builtin_clz(i)));
			for (int j = i - hb; j >= 0; j = (j - 1) & i) {
				int jj = j + hb;
				res += dp[k][i ^ jj] * rf[jj];
				if (cnt ++ == 17) {
					res %= MOD;
					cnt = 0;
				}
				if (j == 0) break;
			}
			res %= MOD;
		}
	}
	int ans = 0;
	for (int i = 0; i <= K; ++ i) (ans += 1LL * f[n] * dp[i][n] % MOD * permute(K, i) % MOD) %= MOD;
	printf("%d\n", (mpow(K, n) + MOD - ans) % MOD);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <assert.h>
using namespace std;

/*
p = i * r + x = 0 (% p)
-i * r * inv(x) = 1 (% p)
inv(i) = -r * inv(x) (% p)
*/

const int N = 250010;
long long fac[N], inv[N];
long long dp[30][N];
int bit[N];
long long n, m, mod;

long long com(int n, int m) {
    return fac[n] * inv[n - m] % mod * inv[m] % mod;
}

void add(long long &x, long long y) {
    x += y;
    if (x >= mod) x %= mod;
}

int main()
{
    cin >> n >> m >> mod;
    inv[0] = fac[0] = inv[1] = fac[1] = 1;
    for (int i = 2; i <= n; i++) {
        int r = mod / i;
        int x = mod - i * r;
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod -1LL * r * inv[x] % mod;
    }
    for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] * inv[i] % mod;
    long long ans = 1;
    for (int i = 1; i <= n; i++) ans = ans * m % mod;
    for (int i = 1; i <= n; i++) bit[i] = bit[i >> 1] + (i & 1);
    if (n & 1) {
        cout << ans << endl;
        return 0;
    }
    dp[0][0] = 1;
    for (int i = 0; i < min(0LL + bit[n], m); i++) {
        for (int j = 0; j < n; j++) {
            if (!dp[i][j]) continue;
            int x = n ^ j;
            for (int k = x, r; k > j; k = (k - 1) & x) {
                r = k | j;
                add(dp[i + 1][r], dp[i][j] * inv[k]);
            }
        }
    }
    for (int i = 1; i <= m; i++)
        ans -= dp[i][n] * fac[m] % mod * inv[m - i] % mod * fac[n] % mod;
    ans = (mod + ans % mod) % mod;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define U 20
#define MAXN 300000
using namespace std;
inline int read(){
	int x=0,f=1;
	char 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 F[U][MAXN],G[U][MAXN];
int cnt[MAXN],fac[MAXN],ifac[MAXN];
int MOD;
int n,m,num,k;
void Multiply(long long F[][MAXN],long long G[][MAXN]){
	for (int s=m;s>=0;--s){
		for (int i=n;i>=0;--i){
			for (int j=0;j<i;++j){
				F[i][s]=(F[i][s]+1ll*F[j][s]*G[i-j][s]%MOD)%MOD;
			}
		}
	}
}
inline int ksm(int b,int k){
    int ans=1;
    while (k){
        if (k&1) ans=1ll*ans*b%MOD;
        b=1ll*b*b%MOD;
        k>>=1;
    }
    return ans;
}
void FWT_or(long long *f,int opr){
    int maxn=m;
	for (int len=1;len<maxn;len*=2){
		for (int i=0;i<maxn;i+=len*2){
			for (int j=0;j<len;++j){
				int x=f[i+j],y=f[i+j+len];
				if (opr==1){
					f[i+j]=x;
					f[i+j+len]=(x+y)%MOD;
				}
				else {
					f[i+j]=x;
					f[i+j+len]=(y-x+MOD)%MOD;
				}
			}
		}
	}
}
#define lowbit(x) (x&(-x))
void Init(){
	cnt[0]=0;
	for (int i=1;i<MAXN;++i) cnt[i]=cnt[i-lowbit(i)]+1;
	fac[0]=1;
	for (int i=1;i<MAXN;++i) fac[i]=1ll*fac[i-1]*i%MOD;
	for (int i=0;i<MAXN;++i) ifac[i]=ksm(fac[i],MOD-2);
}
int main(){
	num=read(),k=read();
	MOD=read();
	Init();
	if (num&1){
		printf("%d\n",ksm(k,num));
		return 0;
	}
	n=log2(num)+1,m=(1<<n);
	for (int i=0;i<m;++i) F[cnt[i]][i]=ifac[i];
	for (int i=0;i<=n;++i) FWT_or(F[i],1);
	G[0][0]=1,FWT_or(G[0],1);
	int t=k;
	while (t){
		if (t&1){
        	for (register int i=n;i>=0;--i){
        		for (register int j=0;j<i;++j){
        	        for (register int s=0;s<m;++s){
        				G[i][s]=(G[i][s]+G[j][s]*F[i-j][s])%MOD;
        			}
        		}
        	}
		}
    	for (register int i=n;i>=0;--i){
    		for (register int j=0;j<i;++j){
    	        for (register int s=0;s<m;++s){
    				F[i][s]=(F[i][s]+F[j][s]*F[i-j][s])%MOD;
    			}
    		}
    	}
		t>>=1;
	}
	FWT_or(G[cnt[num]],-1);
	// cerr<<G[cnt[num]][num]<<endl;
	printf("%d\n",((ksm(k,num)-1ll*G[cnt[num]][num]*fac[num]%MOD)%MOD+MOD)%MOD);
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MN 250005
int Mod;
int ksm(int a,int x){
	LL ans=1,w=a;
	while(x){
		if(x&1)ans=ans*w%Mod;
		w=w*w%Mod;
		x>>=1;
	}
	return ans;
}
int n,k;
int fac[MN],dfac[MN],inv[MN],f[19][MN];
int solve(int use,int S){
	if(f[use][S]!=-1)return f[use][S];
	int ret=0;
	if(!S){
		int ret=fac[n];
		for(int i=1;i<=use;++i)
			ret=1LL*ret*(k-i+1)%Mod;
//		cerr<<"!S "<<use<<" "<<S<<": "<<ret<<endl;
		return f[use][S]=ret;
	}
	if(use>k)return 0;
	int lbit=S&(-S);
	int U=S-lbit;
	for(int T=U;;T=(T-1)&U){
//		cerr<<"? "<<S<<" "<<T<<" "<<lbit<<endl;
		ret=(1LL*dfac[S-T]*solve(use+1,T)+ret)%Mod;
		if(!T)break;
	}
//	cerr<<use<<" "<<S<<": "<<ret<<endl;
	return f[use][S]=ret;
}
int main(){
	scanf("%d%d%d",&n,&k,&Mod);
	if(n&1){printf("%d\n",ksm(k,n));return 0;}
	fac[0]=dfac[0]=fac[1]=dfac[1]=inv[1]=1;
	for(int i=2;i<=n;++i){
		fac[i]=1ll*fac[i-1]*i%Mod;
		inv[i]=1ll*(Mod-Mod/i)*inv[Mod%i]%Mod;
		dfac[i]=1ll*dfac[i-1]*inv[i]%Mod;
	}
	for(int i=0;i<=18;++i)
		for(int j=0;j<=n;++j)
			f[i][j]=-1;
	printf("%d\n",(ksm(k,n)-solve(0,n)+Mod)%Mod);
	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<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int N=300010;
//const int N=20;
int n,K,mod;
int dp[N][20];
int cnt[N],mx[N];
int cj[N],inv[N];

int power(int x,int y){
	int ret=1;
	for(;y;y>>=1,x=1ll*x*x%mod){
		if(y&1)	ret=1ll*ret*x%mod;
	}
	return ret;
}

int C(int i,int j){
	return 1ll*cj[i]*inv[j]%mod*inv[i-j]%mod;
}

int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	scanf("%d%d%d",&n,&K,&mod);
	int i,j,k;int ans=0;int down,up,tmp,now,cur1,cur2;
	if(n&1){printf("%d\n",power(K,n));return 0;}
	cj[0]=1;rep(i,1,n)	cj[i]=1ll*cj[i-1]*i%mod;
	inv[n]=power(cj[n],mod-2);inv[0]=1;
	for(i=n-1;i;i--)	inv[i]=1ll*inv[i+1]*(i+1)%mod;
	rep(i,1,n)
		rep(j,0,18)
			if(1<<j&i)	cnt[i]++;
	rep(i,1,n)
		for(j=18;j>=0;j--)
			if(1<<j&i){mx[i]=1<<j;break;}
	dp[0][0]=1;
	rep(i,1,n){
		if((n&i)!=i)	continue;
		now=i^mx[i];
		for(k=now;k>=0;k=(k-1)&now){
			cur1=k+mx[i];cur2=i^cur1;
			up=min(K-1,cnt[cur2]);
			tmp=C(i,cur1);
			rep(j,0,up){
				dp[i][j+1]=(dp[i][j+1]+1ll*dp[cur2][j]*tmp)%mod;
			}
			if(k==0)	break;
		}
	}
	rep(j,1,min(cnt[n],K))	ans=(ans+1ll*dp[n][j]*C(K,j)%mod*cj[j])%mod;
	ans=(power(K,n)-ans+mod)%mod;
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define N (250005)

using namespace std;

int f[N][18],c[18],ans[18],lg,m,n,k,rhl;
int fac[N],ifac[N],inv[N],cnt[N],v[N];

il void add(int *a,int *b,RG int fg){
  for (RG int i=0;i<lg;++i){
    a[i]+=fg?b[i]:(rhl-b[i]);
    if (a[i]>=rhl) a[i]-=rhl;
  }
  return;
}

il void mul(int *a,int *b){
  memset(c,0,sizeof(c));
  for (RG int i=0;i<lg;++i){
    if (!a[i]) continue;
    for (RG int j=0;i+j<lg;++j)
      c[i+j]=(1LL*a[i]*b[j]+c[i+j])%rhl;
  }
  memcpy(a,c,sizeof(c)); return;
}

il void fpm(int *a,RG int b){
  memset(ans,0,sizeof(ans)),ans[0]=1;
  while (b){
    if (b&1) mul(ans,a);
    if (b>>=1) mul(a,a);
  }
  memcpy(a,ans,sizeof(ans)); return;
}

il int qpow(RG int a,RG int b){
  RG int ans=1;
  while (b){
    if (b&1) ans=1LL*ans*a%rhl;
    if (b>>=1) a=1LL*a*a%rhl;
  }
  return ans;
}

int main(){
#ifndef ONLINE_JUDGE
  freopen("game.in","r",stdin);
  freopen("game.out","w",stdout);
#endif
  cin>>n>>k>>rhl; if (n&1) cout<<qpow(k,n),exit(0);
  fac[0]=ifac[0]=f[0][0]=1; for (m=1;m<=n;m<<=1) ++lg;
  for (RG int i=1;i<=n;++i){
    inv[i]=i==1 ? 1 : 1LL*(rhl-rhl/i)*inv[rhl%i]%rhl;
    fac[i]=1LL*fac[i-1]*i%rhl,ifac[i]=1LL*ifac[i-1]*inv[i]%rhl;
    cnt[i]=cnt[i>>1]+(i&1),f[i][cnt[i]]=ifac[i];
  }
  v[n]=1;
  for (RG int i=n;i;--i){
    if (!v[i]) continue;
    for (RG int j=i;j;(--j)&=i) v[j]=1;
  }
  for (RG int i=1;i<m;i<<=1)
    for (RG int j=0;j<=n;++j) if (j&i) add(f[j],f[j^i],1);
  for (RG int i=0;i<=n;++i) if (v[i]) fpm(f[i],k);
  for (RG int i=1;i<m;i<<=1)
    for (RG int j=0;j<=n;++j) if (j&i) add(f[j],f[j^i],0);
  cout<<(qpow(k,n)-1LL*f[n][cnt[n]]*fac[n]%rhl+rhl)%rhl; return 0;
}


#include<bits/stdc++.h>
#include<tr1/unordered_map>
using namespace std;
inline void R (int &v) {
	static char ch;
	v = 0;
	bool p = 0;
	do {
		ch = getchar();
		if(ch == '-') p = 1;
	} while(!isdigit(ch));
	while(isdigit(ch)) {
		v = (v + (v << 2) << 1) + (ch^'0');
		ch = getchar();
	}
	if(p) v = -v;
}
inline void R (long long &v) {
	static char ch;
	v = 0;
	bool p = 0;
	do {
		ch = getchar();
		if(ch == '-') p = 1;
	} while(!isdigit(ch));
	while(isdigit(ch)) {
		v = (v + (v << 2) << 1) + (ch^'0');
		ch = getchar();
	}
	if(p) v = -v;
}
long long n, k,mod;
int jc[262155];
int inv[262155];
inline int ksm(int x, long long y) {
	int ret = 1;
	while(y) {
		if(y & 1) {
			ret = (long long)ret * x % mod;
		}
		x = (long long) x * x % mod;
		y >>= 1;
	}
	return ret;
}
int dp[262155][31];
bool vis[262155][31];
int Log[262155];
inline int getcnt(int x) {
	int cnt = 0;
	while(x && x % 2==0) {
		++cnt;
		x /= 2;
	}
	return cnt;
}
int jc2[262155];
inline int C(int n, int m) {
	return (long long)jc[n] * inv[m] % mod * inv[n - m] % mod;
}
inline int dfs(int n, int k) {
	if(n == 0) return 1;
	if(k == 0) return 0;
	if(dp[n][k] != -1) {
		return dp[n][k];
	}
	int di = (1 << (Log[n])), ret = 0;
	for(register int i = n; i >= di; i -= 2) {
		ret = ((long long)ret + (((long long)C(n, i) * dfs(n - i, k - 1)) % mod) * k) % mod;
	}
	return dp[n][k] = ret;
}
vector<pair<int, int> >tp;
inline int bfs(int n, int k) {
	static queue<pair<int, int> > que;
	que.push(make_pair(n, k));
	while(!que.empty()) {
		n = que.front().first, k = que.front().second;
		que.pop();
		tp.push_back(make_pair(n, k));
		if(n == 0 || k == 0) continue;
		int di = (1 << (Log[n]));
		for(register int i = n; i >= di; i -= 2) {
			if(jc2[i] + jc2[n - i] == jc2[n] && !vis[n - i][k - 1]){
				vis[n - i][k - 1] = 1;
				que.push(make_pair(n - i, k - 1));
			}
		}
	}
	for(int i = tp.size() - 1; i >= 0; --i) {
		n = tp[i].first, k = tp[i].second;
		if(n == 0) {
			dp[n][k] = 1; continue;
		}
		if(k == 0) {
			dp[n][k] = 0;continue;
		}
		int di = (1 << (Log[n]));
		for(register int i = n; i >= di; i -= 2) {
			if(jc2[i] + jc2[n - i] == jc2[n])
			dp[n][k] = ((long long)dp[n][k] + (((long long)C(n, i) * dp[n - i][k - 1]) % mod) * k) % mod;
		}
	}
	return dp[n][k];
}
int main() {
//	cout<<sizeof(dp);
//	cout<<(1 << ((int)log2(30)));
	R(n), R(k),R(mod);
	jc[0] = 1;
	memset(vis, 0, sizeof(vis));
	Log[0] = -1;
	for(int i = 1; i <= 262150; ++i) jc[i] = (long long)jc[i - 1] * i % mod, Log[i] = Log[i >> 1] + 1, jc2[i] = jc2[i - 1] + getcnt(i);
	inv[262150] = ksm(jc[262150], mod - 2);
	for(int i = 262149; i; --i) inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
//	cout << inv[7]<<'\n';
	inv[0] = inv[1];
	if(n & 1) {
		cout << ksm(k, n) << '\n';
	} else {
		int cnt = 0;
		for(int i = 0; i < 62; ++i) {
			if(n & (1ll<<i)) ++cnt;
		}
		int fei = 0, now = 1;
		int ans = ksm(k, n);

		cout << ((ans - bfs(n, k)) % mod + mod) % mod;
	/*	for(int i = 0; i <= n; ++i) {
			for(int j = 0; j <= k; ++j) {
				printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
			}
		}*/
	}
}


#include <bits/stdc++.h>

using namespace std;

int n, m;
vector <int> k;
vector <pair <int, int>> c;

bool check(int d) {
    vector <int> lastDiscount(n + 1, -1);

    for (int i = 0; i < m; i++)
        if (c[i].first <= d)
            lastDiscount[c[i].second] = max(lastDiscount[c[i].second], c[i].first);

    vector <vector <int>> off(d + 1);
    for (int i = 1; i <= n; i++)
        if (lastDiscount[i] != -1)
            off[lastDiscount[i]].push_back(i);

    vector <int> need = k;
    int cur_money = 0;
    for (int cur_day = 1; cur_day <= d; cur_day++) {
        cur_money++;

        if (cur_day > d)
            continue;

        for (auto it: off[cur_day])
            if (cur_money >= need[it]) {
                cur_money -= need[it];
                need[it] = 0;
            }
            else {
                need[it] -= cur_money;
                cur_money = 0;
                break;
            }
    }

    return accumulate(need.begin(), need.end(), 0) * 2 <= cur_money;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;
    k.resize(n + 1);
    int l = 0, r = 0;
    for (int i = 1; i <= n; i++) {
        cin >> k[i];
        r += k[i];
    }

    c.resize(m);
    for (int i = 0; i < m; i++)
        cin >> c[i].first >> c[i].second;

    r *= 2;
    r++;

    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (check(mid))
            r = mid;
        else
            l = mid;
    }
    cout << r;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int P = 1e9 + 7;
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
long long power(long long x, long long y) {
	if (y == 0) return 1;
	long long tmp = power(x, y / 2);
	if (y % 2 == 0) return tmp * tmp % P;
	else return tmp * tmp % P * x % P;
}
long long inv(long long x) {
	static long long mem[MAXN];
	if (mem[x] != 0) return mem[x];
	else return mem[x] = power(x, P - 2);
}
long long sum(long long lim, int k) {
	if (k == 0) return 2 * lim + 1;
	else if (k == 2) return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * inv(6) % P;
	else if (k == 4) return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * (3 * power(lim, 2) + 3 * lim - 1) % P * inv(30) % P;
	else return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * (3 * power(lim, 4) + 6 * power(lim, 3) - 3 * lim + 1) % P * inv(42) % P;
}
int main() {
	long long ans = 0;
	long long M, m, n;
	read(M); n = sqrt(M); m = M % P;
	for (long long x = -n; x <= n; x++) {
		long long y = sqrt(M - x * x);
		ans += (2) * sum(y, 6) % P;
		ans += (6 * power(x, 2) % P - (3 * m + 6) % P) % P * sum(y, 4) % P;
		ans += (6 * power(x, 4) % P - (6 * m + 12) % P * power(x, 2) + (3 * m + 4)) % P * sum(y, 2) % P;
		ans += (2 * power(x, 6) % P - (3 * m + 6) % P * power(x, 4) % P + (3 * m + 4) % P * power(x, 2) % P + m * (m + 1) % P * (m + 2) % P) % P * sum(y, 0) % P;
		ans %= P;
	}
	ans = (ans + P) % P;
	ans = ans * inv(6) % P;
	writeln(ans);
	return 0;
}


#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>
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;
#define N 200
const ll lim = ll(1e18);
ll dp[N][N], digits[N], base, n;
string number;
vll exponentes;
ll solve(int ind, int xp){
    if(ind == n){
        if(xp < 0) return 0;
        else return -1;
    }
    if(xp < 0) return -1;
    if(dp[ind][xp] != -1) return dp[ind][xp];
    if(number[ind] == '0') return dp[ind][xp] = solve(ind+1, xp-1);
    ll cur = 0; ll best = lim;
    for(int e = ind; e < n; e++){
        cur = (cur*10+(number[e] - '0'));
        if(cur >= base) break;
        if(cur*exponentes[xp] > lim || cur*exponentes[xp] < cur) break;
        ll next = solve(e+1, xp-1);
        if(next == -1) continue;
        best = min(best, exponentes[xp]*cur + next);
    }
    return dp[ind][xp] = best;
}
int main(){
    cin >> base;
    cin >> number;
    n = number.length();
    mset(dp, -1);
    mset(digits, -1);
    ll ans = lim;
    exponentes.pb(1LL);
    while(exponentes.back()*base > exponentes.back())
        exponentes.pb(exponentes.back()*base);
    for(int e = 0; e < exponentes.size(); e++){
        if(solve(0, e) == -1) continue;
        ans = min(ans, solve(0, e));
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int a[20], s[20], c[20];
long long l, r;
vector <int> b;

int solve() {
    int cnt = 0;
    for (int i = 1; i < 10; i++) {
        cnt += a[i];
    }
    if (cnt > b.size()) {
        return 0;
    }
    a[0] = b.size() - cnt;
    int pos = b.size() - 1;
    while (pos >= 0) {
        if (a[b[pos]] > 0) {
            c[pos] = b[pos];
            a[b[pos]]--;
            pos--;
            continue;
        }
        int mv = 10;
        for (int i = 0; i < 10; i++) {
            if (a[i] > 0) {
                mv = i;
                break;
            }
        }
        while (pos < b.size() && mv >= b[pos]) {
            pos++;
            if (pos == b.size()) {
                return 0;
            }
            mv = min(mv, c[pos]);
            a[c[pos]]++;
        }
        int x = b[pos] - 1;
        while (x >= 0 && a[x] == 0) {
            x--;
        }
        c[pos] = x;
        a[x]--;
        pos--;
        x = 9;
        while (pos >= 0) {
            while (x >= 0 && a[x] == 0) {
                x--;
            }
            c[pos] = x;
            a[x]--;
            pos--;
        }
        break;
    }
    long long val = 0;
    for (int i = b.size() - 1; i >= 0; i--) {
        val = val * 10 + c[i];
    }
    return val >= l ? 1 : 0;
}

void dfs(int x, int left, int &ans) {
    if (x == 10) {
        if (left == 18) {
            return;
        }
        for (int i = 0; i < 10; i++) {
            s[i] = a[i];
        }
        ans += solve();
        for (int i = 0; i < 10; i++) {
            a[i] = s[i];
        }
        return;
    }
    for (int i = 0; i <= left; i++) {
        a[x] = i;
        dfs(x + 1, left - i, ans);
    }
}

int main() {

    scanf("%lld %lld", &l, &r);

    while (r > 0) {
        b.push_back(r % 10);
        r /= 10;
    }

    int ans = 0;

    dfs(1, 18, ans);

    printf("%d\n", ans);

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
struct SSP{
	int n, source, sink;
    vector<vector<int> > adj;
	struct edge {
        int u, flow, cap, cost, origcost;
    };
	vector<edge> edges;
	SSP(int n, int source, int sink): n(n),source(source),sink(sink),adj(n) {}
    int add_edge(int a, int b, int cap, int cost){
        int result = edges.size();
        adj[a].push_back(edges.size());
        edges.push_back({b, 0, cap, cost, cost});
        adj[b].push_back(edges.size());
        edges.push_back({a, 0, 0, -cost, -cost});
        return result;
    }
    pair<int, int> djk(){
        struct state_t{
            int dist, prev, maxf;
        };
        priority_queue<pair<int, int>,vector<pair<int, int> >, greater<pair<int, int> > > pq;
        vector<state_t> states(n, {INT_MAX/2, -1, 0});
        states[source] = {0, -2, INT_MAX};
        pq.push(make_pair(states[source].dist, source));
        while(!pq.empty()){
            int d, v; tie(d, v) = pq.top(); pq.pop();
            if(d != states[v].dist) continue;
            for(int edid : adj[v]){
                edge & ed = edges[edid];
                if(ed.flow < ed.cap && states[ed.u].dist > states[v].dist + ed.cost){
                    states[ed.u].dist = states[v].dist + ed.cost;
                    states[ed.u].prev = edid;
                    states[ed.u].maxf = min(states[v].maxf, ed.cap - ed.flow);
                    pq.push(make_pair(states[ed.u].dist, ed.u));
                }
            }
        }
        int lastid = states[sink].prev, extraflow = states[sink].maxf, atcost = 0;
        if(lastid == -1) return {0, 0};
        for(int id = lastid; id != -2; id = states[edges[id^1].u].prev){
            edges[id].flow += extraflow;
            edges[id^1].flow -= extraflow;
            atcost += edges[id].origcost;
        }
        for(int id = 0; id < (int) edges.size(); id++){
            int v = edges[id^1].u, u = edges[id].u;
            if(states[v].prev != -1 && states[u].prev != -1)
                edges[id].cost += states[v].dist - states[u].dist;
        }
        return {extraflow, atcost};
    }
    pair<int, int> mfmc(){
        int flow = 0, cost = 0;
        if(any_of(edges.begin(), edges.end(), [](const edge & ed){
            return ed.flow > 0 && ed.cost < 0;
        })) bellamanford();
        int extraflow, atcost;
        while(tie(extraflow, atcost) = djk(), extraflow > 0)
            flow += extraflow, cost += extraflow * atcost;
        return {flow, cost};
    }
    void bellamanford(){
        vector<int> dist(n, INT_MAX/2);
        dist[source] = 0;
        for(int rep = 0; rep < n; rep++){
            bool changed = false;
            for(int id = 0; id < (int)edges.size(); id++){
                int v = edges[id^1].u, u = edges[id].u;
                if(edges[id].flow < edges[id].cap && dist[v] != INT_MAX/2 &&
                    dist[u] > dist[v] + edges[id].cost){
                    dist[u] = dist[v] + edges[id].cost;
                    changed = true;
                    assert(rep < n - 1);
                }
            }
            if(!changed) break;
        }
        for(int id = 0; id < (int) edges.size(); id++){
            int v = edges[id^1].u, u = edges[id].u;
            if(dist[v] < INT_MAX/2 && dist[u] < INT_MAX/2)
                edges[id].cost += dist[v] - dist[u];
        }
    }
};
const int maxn = 2521;
int to[maxn][maxn], gp[maxn];
vector<int> e[maxn];
int main(){
	int n, m, k, c, d; cin >> n >> m >> k >> c >> d;
	const int max_time = 120;
	vector<int> a(k); for(int i = 0; i < k; i++) cin >> a[i];
	for(int i = 0; i < m; i++){
		int x, y; cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	int nd = 0;
	for(int i = 0; i < max_time; i++){
		for(int j = 1; j <= n; j++) to[i][j] = nd++;
	}
	int S = nd++, T = nd++;
	SSP flow(nd, S, T);
	for(int i = 0; i < k; i++) flow.add_edge(S, to[0][a[i]], 1, 0);
	for(int i = 0; i < max_time; i++) flow.add_edge(to[i][1], T, k, c * i);
	for(int i = 0; i < max_time - 1; i++){
		for(int j = 1; j <= n; j++){
			for(int k : e[j]){
				for(int z = 0; z <= 52; z++){
					flow.add_edge(to[i][j], to[i + 1][k], 1, d * (2 * z + 1));
				}
			}
			flow.add_edge(to[i][j], to[i + 1][j], k, 0);
		}
	}
	int p0, p1; tie(p0, p1) = flow.mfmc();
	cout << p1 << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 10000,M = N * 100;
const long long inf = LLONG_MAX >> 5;
class FlowGraph
{
private:
    int head[N],v[M],nxt[M],c[M],ptr,t;
    long long w[M],cost;
    inline void padd_edge(int uu,int vv,long long ww,int cc)
    {
        v[ptr] = vv;w[ptr] = ww;c[ptr] = cc;
        nxt[ptr] = head[uu];head[uu] = ptr++;
    }
    int dis[N],id[N],cur[N];bool vis[N];
    bool spfa(int s,int t)
    {
        memset(vis,0,sizeof(vis));
        memset(dis,-1,sizeof(dis));
        queue<int> q;q.push(s);
        dis[s] = 0;id[s] = 1;
        while (!q.empty())
        {
            int nd = q.front();
			q.pop();vis[nd] = 0;
            for (int i = head[nd];i != -1;i = nxt[i])
                if (w[i] && (dis[v[i]] == -1 || dis[v[i]] > dis[nd] + c[i]))
                {
                    dis[v[i]] = dis[nd] + c[i];
                    if (!vis[v[i]])
                    {
                        vis[v[i]] = 1;q.push(v[i]);
                    }
                }
        }
        return dis[t] != -1;
    }
    long long dfs(int pos,long long flow)
    {
        if (pos == t) return flow;
        vis[pos] = 1;
        long long tmp = flow;
        for (int i = cur[pos] != -1 ? cur[pos] : head[pos];i != -1;i = nxt[i])
            if (!vis[v[i]] && w[i] && dis[v[i]] == dis[pos] + c[i])
            {
                cur[pos] = i;
                long long fl = dfs(v[i],min(flow,w[i]));
                cost += fl * c[i];
                w[i] -= fl;w[i ^ 1] += fl;
                flow -= fl;
                if (!flow) break;
            }
        return tmp - flow;
    }
public:
    FlowGraph()
    {memset(head,-1,sizeof(head));memset(nxt,-1,sizeof(nxt));}
    inline void add_edge(int uu,int vv,long long ww,int cc)
    {
        padd_edge(uu,vv,ww,cc);padd_edge(vv,uu,0,-cc);
    }
    long long dinic(int s,int t)
    {
        cost = 0;this->t = t;
        while (spfa(s,t))
        {
            memset(cur,-1,sizeof(cur));dfs(s,inf);
        }
        return cost;
    }
};
FlowGraph g;
int n,m,kk,c,d,a[55];
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n >> m >> kk >> c >> d;
    for (int i = 1;i <= kk;i++) cin >> a[i];
    int s = 0,t = n * 100 + 1;
    for (int i = 1;i <= 100;i++) g.add_edge(i,t,inf,0);
    for (int i = 1;i <= kk;i++) g.add_edge(s,(a[i] - 1) * 100 + 1,1,0);
    for (int i = 2;i <= n;i++)
        for (int j = 1;j < 100;j++)
            g.add_edge((i - 1) * 100 + j,(i - 1) * 100 + j + 1,inf,c);
    int t1,t2;
    for (int _ = 1;_ <= m;_++)
    {
        cin >> t1 >> t2;
        for (int i = 1;i < 100;i++)
            for (int j = 1;j <= kk;j++) //其实不用枚举到kk，枚举到25左右即可
            {
                int val = c + d * (j * j - (j - 1) * (j - 1));
                g.add_edge((t1 - 1) * 100 + i,(t2 - 1) * 100 + i + 1,1,val);
                g.add_edge((t2 - 1) * 100 + i,(t1 - 1) * 100 + i + 1,1,val);
            }
    }
    cout << g.dinic(s,t) << endl;
    return 0;
}
// 最小费用流 珂以利用流量来处理多人走同一条路的情况


#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int Z = 100;
const int N = Z * Z;
const int M = Z * Z * Z * 3;
int px[N], py[N];
int head[N], to[M], ct[M], w[M], nt[M];
int d[N], vis[N];
int pre[N], id[N];
int s, t, tot;

void add(int u, int v, int flow, int cost){
    to[tot] = v; ct[tot] = cost;
    w[tot] = flow; nt[tot] = head[u]; head[u] = tot++;

    to[tot] = u; ct[tot] = -cost;
    w[tot] = 0; nt[tot] = head[v]; head[v] = tot++;
}
void init(){
    memset(head, -1, sizeof(head));
    tot = 0;
}
int spfa(int s, int t){
    queue<int> q;
    memset(d, inf, sizeof(d));
    memset(vis, 0, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    d[s] = 0;
    q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        vis[u] = 0;
//        cout << u << " " << d[u] << endl;
        for(int i = head[u]; ~i; i = nt[i]){
            if(w[i] > 0 && d[to[i]] > d[u] + ct[i]){
                d[to[i]] = d[u] + ct[i];
                pre[to[i]] = u;
                id[to[i]] = i;
//                if(to[i] == 201) cout << "Updata " << u << " " << to[i] <<" " << d[to[i]] <<endl;
                if(!vis[to[i]]){
                    vis[to[i]] = 1;
                    q.push(to[i]);
                }
            }
        }

    }
//    cout << d[t] << "? "<< endl;
    return d[t] < inf;
}
int MaxFlow(int s, int t){
    int Mi = inf;
    int sum = 0;
    int tt = 0;
//    cout << s << " ss " << t  << endl;
    while(spfa(s, t)){
        Mi = inf;
        for(int i = t; i != s; i = pre[i])
            Mi = min(Mi, w[id[i]]);
        for(int i = t; i != s; i = pre[i]){
            w[id[i]] -= Mi;
            w[id[i]^1] += Mi;
        }
        tt += Mi;
        sum += d[t] * Mi;
//        cout << d[t] << endl;
//        cout << "asd" << endl;
    }
    return sum;
}
int main(){
    init();
    int n, m, k, c, d;
    scanf("%d%d%d%d%d", &n, &m, &k, &c, &d);
    int s = 0, t = n * 100 + 1;
    for(int i = 1; i <= 100; ++i)
        add(i, t, 100, 0);
    for(int i = 1, v; i <= k; ++i){
        scanf("%d" , &v);
        add(s, (v-1)*100+1, 1, 0);
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j < 100; ++j){
            add((i-1)*100 + j, (i-1)*100 + j + 1, 100, c);
        }
    }
    for(int i = 1, u, v; i <= m; ++i){
        scanf("%d%d", &u, &v);
        for(int j = 1; j < 100; ++j){
            int idx = (u-1) * 100 + j;
            int idy = (v-1) * 100 + j + 1;
            for(int k = 1; k <= 50; ++k){
                add(idx, idy, 1, c + (k * k - (k-1) * (k-1))* d) ;
            }
        }
        for(int j = 1; j < 100; ++j){
            int idx = (v-1) * 100 + j;
            int idy = (u-1) * 100 + j + 1;
            for(int k = 1; k <= 50; ++k){
                add(idx, idy, 1, c + (k * k - (k-1) * (k-1))* d) ;
            }
        }
    }
    printf("%d\n", MaxFlow(s, t));
    return 0;
}


#include<algorithm>
#include<cstdio>
#include<queue>
#define Maxn 5500
#define Maxm 35500
#define INF 1000000000
using namespace std;
struct Line
{int nxt,t,c,l;}g[Maxm];
int fir[Maxn],tl=1;
void adl(int f,int t,int c,int cost){
  g[++tl]=(Line){fir[f],t,c,cost};fir[f]=tl;
  g[++tl]=(Line){fir[t],f,0,-cost};fir[t]=tl;
}
int S,T,pre[Maxn],lp[Maxn],flow[Maxn],dis[Maxn];
bool in[Maxn];
queue<int> q;
int spfa()
{
  for (int i=1;i<=T;i++)
    {dis[i]=INF;flow[i]=0;}
  flow[S]=INF;dis[S]=0;
  q.push(S);in[S]=1;
  while(!q.empty()){
    int u=q.front();
    in[u]=0;q.pop();
    for (int i=fir[u],v;i;i=g[i].nxt)
      if (g[i].c&&dis[v=g[i].t]>dis[u]+g[i].l){
        dis[v]=dis[u]+g[i].l;
        flow[v]=min(flow[u],g[i].c);
        lp[v]=i;pre[v]=u;
        if (!in[v]){in[v]=1;q.push(v);}
     }
  }return flow[T];
}
int n,m,k,C,D,pl[105][105],st[105][105];
struct Data
{int f,t;}s[105];
#define P(x,y) ((x)*n+(y))
int main()
{
  scanf("%d%d%d%d%d",&n,&m,&k,&C,&D);
  S=P(n+k,n)+1;T=S+2;
  adl(P(0,1),T,INF,0);
  for (int i=1,p;i<=k;i++){
    scanf("%d",&p);
    adl(S,P(0,p),1,0);
  }for (int i=1;i<=m;i++){
    scanf("%d%d",&s[i*2].f,&s[i*2].t);
    s[i*2-1]=(Data){s[i*2].t,s[i*2].f};
  }m*=2;
  for (int t=1;t<=n+k;t++){
    for (int i=1;i<=n;i++)
      adl(P(t-1,i),P(t,i),INF,0);
    for (int i=1;i<=m;i++){
      adl(P(t-1,s[i].f),P(t,s[i].t),1,D);
      pl[t][i]=tl;st[t][i]=1;
    }adl(P(t,1),T,INF,t*C);
  }
  int sav,cans=0;
  while(sav=spfa()){
    int u=T;
    while(u!=S){
      int las=pre[u];
      g[lp[u]].c-=sav;
      g[lp[u]^1].c+=sav;
      u=las;
    }cans+=dis[T]*sav;
    for (int t=1;t<=n+n;t++)
      for (int i=1;i<=m;i++)
        if (g[pl[t][i]].c){
          st[t][i]++;
          adl(P(t-1,s[i].f),P(t,s[i].t),1,D*(st[t][i]*2-1));
          pl[t][i]=tl;
        }
  }printf("%d\n",cans);
  return 0;
}


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

//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair((a),(b))
#define x first
#define y second
#define bg begin()
#define ed end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define R(i,a,b) for(int i=(a),i##E=(b);i<i##E;i++)
#define L(i,a,b) for(int i=(b)-1,i##E=(a)-1;i>i##E;i--)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//Data
const int N=50;
int n,m,k,c,d,mxt;

//Flows
const int fN=(N*3+1)*N+2;
int s,t,fn;
vector<int> e[fN],to,fw,co;
void adde(int u,int v,int w,int c){
    e[u].pb(sz(to)),to.pb(v),fw.pb(w),co.pb(+c);
    e[v].pb(sz(to)),to.pb(u),fw.pb(0),co.pb(-c);
}
int dep[fN],pre[fN]; bool vis[fN];
bool spfa(){
    fill(dep,dep+fn,iinf),fill(pre,pre+fn,-1),fill(vis,vis+fn,false);
    queue<int> q; q.push(s),vis[s]=true,dep[s]=0;
    while(sz(q)){
        int u=q.front(); q.pop(),vis[u]=false;
        for(int v:e[u])if(fw[v]&&dep[to[v]]>dep[u]+co[v])
            dep[to[v]]=dep[u]+co[v],pre[to[v]]=v,
            (!vis[to[v]])&&(q.push(to[v]),vis[to[v]]=true);
    }
    return dep[t]<iinf;
}
pair<int,int> mcmf(){
    pair<int,int> res(0,0);
    while(spfa()){
        int f=iinf;
        for(int u=t;u!=s;u=to[pre[u]^1]) f=min(f,fw[pre[u]]);
        for(int u=t;u!=s;u=to[pre[u]^1]) fw[pre[u]]-=f,fw[pre[u]^1]+=f;
        res.x+=f,res.y+=dep[t]*f;
    }
    return res;
}

//Main
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>k>>c>>d,mxt=N*3+1;
    fn=(t=(s=n*mxt)+1)+1;
    R(i,0,k){int u;cin>>u,--u,adde(s,u,1,0);}
    while(m--){
        int u,v; cin>>u>>v,--u,--v;
        R(i,0,mxt-1)R(p,1,k+1)
            adde(i*n+u,(i+1)*n+v,1,2*d*p-d),
            adde(i*n+v,(i+1)*n+u,1,2*d*p-d);
    }
    R(u,0,n)R(i,0,mxt-1) adde(i*n+u,(i+1)*n+u,k,0);
    R(i,0,mxt) adde(i*n,t,k,i*c);
    pair<int,int> ans=mcmf();
    assert(ans.x==k);
    cout<<ans.y<<'\n';
    return 0;
}


/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
const string FILENAME = "input";
const int MAXN = 8200;
const long long INF = 1e14;

struct edge {
	int u, v;
	long long cap, flow;
	long long cost;
	long long rem() {
		return cap - flow;
	}
	edge(){}
	edge(int _u, int _v, long long _cap, long long _flow, long long _cost) {
		u = _u;
		v = _v;
		cap = _cap;
		flow = _flow;
		cost = _cost;
	}
};

int nodes;
vector<int> g[MAXN];
vector<edge> e;

void add_edge(int u, int v, long long cap, long long cost) {
	g[u].pb(sz(e));
	e.pb(edge(u, v, cap, 0, cost));
	g[v].pb(sz(e));
	e.pb(edge(v, u, 0, 0, -cost));
}

long long dist[MAXN], mncost;
int pre[MAXN];
long long cap[MAXN], mxflow;
bool inq[MAXN];

void flow(int s, int t) {
	memset(inq, 0, sizeof(inq));
	mxflow = mncost = 0;
	while (true) {
		fill(dist, dist + nodes, INF);
		dist[s] = 0;
		memset(pre, -1, sizeof(pre));
		pre[s] = 0;
		memset(cap, 0, sizeof(cap));
		cap[s] = INF;
		queue<int> q;
		q.push(s);
		inq[s]=1;
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			inq[u] = 0;
			for (int it = 0; it < sz(g[u]); it++) {
				int i = g[u][it];
				edge &f = e[i];
				if (f.rem() && dist[f.v] > dist[u] + f.cost + 1e-9) {
					dist[f.v] = dist[u] + f.cost;
					pre[f.v] = i;
					cap[f.v] = min(cap[u], f.rem());
					if (!inq[f.v]) {
						q.push(f.v);
						inq[f.v] = 1;
					}
				}
			}
		}
		if (pre[t] < 0) {
			break;
		}
		mxflow +=cap[t];
		mncost += cap[t] * dist[t];
		int v = t;
		while (v != s) {
			e[pre[v]].flow += cap[t];
			e[pre[v] ^ 1].flow -= cap[t];
			v = e[pre[v]].u;
		}
	}
}

int n, m, k, c, d;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//read(FILENAME);
	cin >> n >> m >> k >> c >> d;
	for (int i = 0; i < k; i++) {
		int x;
		cin >> x;
		x--;
		add_edge(0, x + 1, 1, 0);
	}
	for (int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		x--, y--;
		for (int i = 0; i < 105; i++) {
			for (int j = 0; j < 51; j++) {
				add_edge(i * n + x + 1, (i + 1) * n + y + 1, 1, c + d * (2 * j + 1));
				add_edge(i * n + y + 1, (i + 1) * n + x + 1, 1, c + d * (2 * j + 1));
			}
		}
	}
	for (int i = 0; i < 105; i++) {
		for (int j = 0; j < n; j++) {
			add_edge(i * n + j + 1, (i + 1) * n + j + 1, 51, c);
		}
	}
	for (int i = 0; i < 105; i++) {
		add_edge(i * n + 1, 105 * n + 1, 51, 0);
	}
	nodes = 6 + 107 * n;
	flow(0, 105 * n + 1);
	cout << mncost << '\n';
	return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using i64=int_least64_t;

namespace std
{
    // hash
    template <class T> size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); }
    template <class T, class U> struct hash<pair<T, U>> { size_t operator()(pair<T, U> const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } };
    template <class tuple_t, size_t index = tuple_size<tuple_t>::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc<tuple_t, index - 1>::apply(seed, t), get<index>(t)); } };
    template <class tuple_t> struct tuple_hash_calc<tuple_t, 0> { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } };
    template <class... T> struct hash<tuple<T...>> { size_t operator()(tuple<T...> const &t) const { return tuple_hash_calc<tuple<T...>>::apply(0, t); } };
    // iostream
    template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; }
    template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; }
    template <class tuple_t, size_t index> struct tupleis { static istream &apply(istream &is, tuple_t &t) { tupleis<tuple_t, index - 1>::apply(is, t); return is >> get<index>(t); } };
    template <class tuple_t> struct tupleis<tuple_t, SIZE_MAX> { static istream &apply(istream &is, tuple_t &t) { return is; } };
    template <class... T> istream &operator>>(istream &is, tuple<T...> &t) { return tupleis<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(is, t); }
    template <> istream &operator>>(istream &is, tuple<> &t) { return is; }
    template <class tuple_t, size_t index> struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { tupleos<tuple_t, index - 1>::apply(os, t); return os << ' ' << get<index>(t); } };
    template <class tuple_t> struct tupleos<tuple_t, 0> { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } };
    template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) { return tupleos<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(os, t); }
    template <> ostream &operator<<(ostream &os, const tuple<> &t) { return os; }
    template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr>
    istream& operator>>(istream& is, Container &cont) { for(auto&& e : cont) is >> e; return is; }
    template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr>
    ostream& operator<<(ostream& os, const Container &cont) { bool flag = 1; for(auto&& e : cont) flag ? flag = 0 : (os << ' ', 0), os << e; return os; }
} // namespace std

#include <cassert>
#include <list>
#include <vector>

// Base class of flow algorithms
template <class cap_t, class cost_t>
struct flow_base
{
    struct edge_t
    {
        size_t src, dst;
        cap_t cap; cost_t cost;
        edge_t *rev;
        edge_t(size_t _src, size_t _dst, cap_t _cap, cost_t _cost, edge_t *_rev)
            : src(_src), dst(_dst), cap(_cap), cost(_cost), rev(_rev) {}
    }; // struct edge_t

protected:
    using adj_type = std::vector<std::list<edge_t>>;
    adj_type adj;

public:
    flow_base(size_t _n = 0) : adj(_n) {}

    flow_base(const flow_base& other) : adj(other.size())
    {
        for(size_t node{0}; node != other.size(); ++node)
        {
            for(const auto &e : other[node])
            {
                if(e.src == node)
                {
                    adj[e.src].emplace_back(e);
                    adj[e.dst].emplace_back(*e.rev);
                    adj[e.src].back().rev = &adj[e.dst].back();
                    adj[e.dst].back().rev = &adj[e.src].back();
                    e.rev->src = -1;
                }
                else
                {
                    e.rev->rev->src = node;
                }
            }
        }
    }

    flow_base &operator=(const flow_base &other)
    {
        if(this != &other)
        {
            flow_base cop(other);
            std::swap(adj, cop.adj);
        }
        return *this;
    }

    size_t size() const { return adj.size(); }

    typename decltype(adj)::reference operator[](size_t node) { assert(node < size()); return adj[node]; }
    typename decltype(adj)::const_reference operator[](size_t node) const { assert(node < size()); return adj[node]; }

    void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)
    {
        assert(src < size()); assert(dst < size());
        if(cap <= 0) return;
        adj[src].emplace_back(src, dst, cap, cost, nullptr);
        adj[dst].emplace_back(dst, src, 0, -cost, nullptr);
        adj[src].back().rev = &adj[dst].back();
        adj[dst].back().rev = &adj[src].back();
    }
}; // struct flow_base

template <class cap_t, class cost_t>
class min_cost_flow : public flow_base<cap_t, cost_t>
{
    using base = flow_base<cap_t, cost_t>;
    using edge_t = typename base::edge_t;

    template <class type>
    constexpr bool valid(const type &val) const { return val != static_cast<type>(-1); }

    template <class type>
    constexpr void make_invalid(type &val) const { val = -1; }

    cost_t min_cost, total_cost;
    std::vector<cap_t> supp;
    std::vector<cost_t> ptnl;

    void copy_member(const min_cost_flow &other)
    {
        min_cost = other.min_cost;
        total_cost = other.total_cost;
        supp = other.supp;
        ptnl = other.ptnl;
    }

    void Dijkstra(std::vector<edge_t*> &last)
    {
        const cost_t infty(total_cost + 1);
        std::vector<cost_t> dist(base::size(), infty);
        //*/ // O((V + E)logV)
        std::priority_queue<std::pair<cost_t, size_t>> que;
        for(size_t node{}; node != base::size(); ++node)
        {
            if(supp[node] > static_cast<cap_t>(0))
            {
                dist[node] = 0;
                que.emplace(0, node);
            }
        }
        while(!que.empty())
        {
            // auto [nsp, node] = que.top();
            cost_t nsp; size_t node;
            std::tie(nsp, node) = que.top();
            que.pop();
            if(-nsp != dist[node]) continue;
            ptnl[node] -= nsp;
            if(ptnl[node] > infty) ptnl[node] = infty;
            make_invalid(dist[node]);
            for(edge_t &e : base::adj[node])
            {
                if(!valid(dist[e.dst]) || e.cap == static_cast<cap_t>(0)) continue;
                if(dist[e.dst] + ptnl[e.dst] > ptnl[node] + e.cost)
                {
                    dist[e.dst] = ptnl[node] + e.cost - ptnl[e.dst];
                    last[e.dst] = &e;
                    que.emplace(-dist[e.dst], e.dst);
                }
            }
        }
        /*/ // O(V^2)
        //*/
    }

public:
    min_cost_flow(size_t _n = 0) : base::flow_base(_n), min_cost(0), total_cost(0), supp(_n), ptnl(_n)  {}

    min_cost_flow(const min_cost_flow &other) : base::flow_base(other)
    {
        copy_member(other);
    }

    min_cost_flow &operator=(const min_cost_flow &other)
    {
        base::operator=(other);
        copy_member(other);
        return *this;
    }

    void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)
    {
        assert(src != dst);
        if(cap <= static_cast<cap_t>(0)) return;
        if(cost < static_cast<cost_t>(0))
        {
            supp[src] -= cap;
            supp[dst] += cap;
            min_cost += cap * cost;
            total_cost -= cap * cost;
            base::add_edge(dst, src, cap, -cost);
        }
        else
        {
            total_cost += cap * cost;
            base::add_edge(src, dst, cap, cost);
        }
    }

    void supply(size_t node, cap_t vol) { supp[node] += vol; }

    bool flow()
    {
        for(bool aug = true; aug; )
        {
            aug = false;
            std::vector<edge_t*> last(base::size());
            Dijkstra(last);
            std::vector<bool> shut(base::size());
            for(size_t dst{}; dst != base::size(); ++dst)
            {
                if(supp[dst] < static_cast<cap_t>(0) and last[dst])
                {
                    cap_t resid{-supp[dst]};
                    size_t src{dst}, block;
                    make_invalid(block);
                    while(last[src] && !shut[src])
                    {
                        if(resid >= last[src]->cap) resid = last[block = src]->cap;
                        src = last[src]->src;
                    }
                    if(shut[src]) block = src;
                    else
                    {
                        if(resid >= supp[src])
                        {
                            resid = supp[src];
                            block = src;
                        }
                        for(size_t node{dst}; node != src; node = last[node]->src)
                        {
                            last[node]->cap -= resid;
                            last[node]->rev->cap += resid;
                        }
                        supp[src] -= resid;
                        supp[dst] += resid;
                        min_cost += ptnl[dst] * resid;
                        aug = true;
                    }
                    if(valid(block))
                    {
                        for(size_t node{dst}; node != block; node = last[node]->src) shut[node] = true;
                        shut[block]=true;
                    }
                }
            }
        }
        for(cap_t s : supp) if(s > static_cast<cap_t>(0)) return false;
        return true;
    }

    cost_t optimal() { assert(flow()); return min_cost; }
};

main()
{
    min_cost_flow<int,i64> mcf(6100);
    int n,m,k,c,d; cin>>n>>m>>k>>c>>d;
    const int tlim=105;
    for(int i=0; i<k; ++i)
    {
        int j; cin>>j; j--;
        mcf.supply(j,1);
    }
    for(int i=0; i<m; ++i)
    {
        int u,v; cin>>u>>v; --u,--v;
        for(int t=0; t<tlim; ++t)
        {
            int nu=u+n;
            int nv=v+n;
            for(int x=0; x<k; ++x)
            {
                mcf.add_edge(u,nv,1,c+d*(x*2+1));
                mcf.add_edge(v,nu,1,c+d*(x*2+1));
            }
            u=nu;
            v=nv;
        }
    }
    for(int i=1; i<n; ++i)
    {
        for(int t=0; t<tlim; t++)
        {
            mcf.add_edge(n*t+i,n*(t+1)+i,k,c);
        }
    }
    const int dst=mcf.size()-1;
    mcf.supply(dst,-k);
    for(int t=0; t<=tlim; ++t) mcf.add_edge(t*n,dst,k,0);
    cout << mcf.optimal() << "\n";
}


#include<bits/stdc++.h>

using namespace std;

struct edge
{
	int y, c, f, cost;
	edge() {};
	edge(int y, int c, int f, int cost) : y(y), c(c), f(f), cost(cost) {};
};

vector<edge> e;

const int N = 14043;
vector<int> g[N];

long long ans = 0;
long long d[N];
int p[N];
int pe[N];
int inq[N];

const long long INF64 = (long long)(1e18);

int s = N - 2;
int t = N - 1;

int rem(int x)
{
	return e[x].c - e[x].f;
}

void push_flow()
{
	for(int i = 0; i < N; i++) d[i] = INF64, p[i] = -1, pe[i] = -1, inq[i] = 0;
	d[s] = 0;
	queue<int> q;
	q.push(s);
	inq[s] = 1;
	while(!q.empty())
	{
		int k = q.front();
		q.pop();
		inq[k] = 0;
		for(auto x : g[k])
		{
			if(!rem(x)) continue;
			int c = e[x].cost;
			int y = e[x].y;
			if(d[y] > d[k] + c)
			{
				d[y] = d[k] + c;
				p[y] = k;
				pe[y] = x;
				if(inq[y] == 0)
				{
					inq[y] = 1;
					q.push(y);
				}
			}
		}
	}
	int cur = t;
//	vector<int> zz(1, cur);
	while(cur != s)
	{
		e[pe[cur]].f++;
		e[pe[cur] ^ 1].f--;
		cur = p[cur];
//		zz.push_back(cur);
	}
//	reverse(zz.begin(), zz.end());
//	for(auto x : zz) cerr << x << " ";
//	cerr << endl;
	ans += d[t];
}

void add_edge(int x, int y, int c, int cost)
{
	g[x].push_back(e.size());
	e.push_back(edge(y, c, 0, cost));
	g[y].push_back(e.size());
	e.push_back(edge(x, 0, 0, -cost));
}

int main()
{
	int n, m, k, c, d;
	cin >> n >> m >> k >> c >> d;
	for(int i = 0; i < k; i++)
	{
		int x;
		cin >> x;
		--x;
		add_edge(s, x, 1, 0);
	}
	int tt = 101;
	for(int i = 0; i < tt; i++)
		add_edge(0 + i * n, t, k, i * c);
	for(int i = 0; i < m; i++)
	{
		int x, y;
		cin >> x >> y;
		--x;
		--y;
		for(int i = 0; i < tt - 1; i++)
			for(int j = 0; j < k; j++)
				add_edge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));
		for(int i = 0; i < tt - 1; i++)
			for(int j = 0; j < k; j++)
				add_edge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));
 	}
 	for(int i = 0; i < n; i++)
 		for(int j = 0; j < tt - 1; j++)
 			add_edge(i + j * n, i + j * n + n, k, 0);
 	for(int i = 0; i < k; i++)
 		push_flow();
 	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ull unsigned long long
#define mp make_pair
const int maxn=5e3+5;
const ll inf=1e9+7;

inline int read() {
    int X = 0,w = 0; char ch = 0;
    while(!isdigit(ch)) {w |= ch == '-';ch = getchar();}
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48),ch=getchar();
    return w ? -X : X;
}
struct edge
{
    int from,to,cap,flow,cost;
    edge(int a,int b,int c,int d,int e)
    {
        from = a,to = b,cap = c,flow = d,cost = e;
    }
};

ll n,m;
int a[maxn];
int startPoint,endPoint;

struct MCMF
{
    int n, m;
    vector<edge>e;
    vector<int>g[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void Init(int n)
    {
        this -> n = n;
        for(int i = 0;i <= n; i++)
            g[i].clear();
        e.clear();
    }
    void add(int from,int to,int cap,int cost)
    {
        e.push_back(edge(from,to,cap,0,cost));
        e.push_back(edge(to,from,0,0,-cost));
        m = e.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bellman(int s,int t,int& flow,int& cost)
    {
        for(int i = 0;i <= t; i++)
            d[i] = inf,inq[i] = 0;
        d[s] = 0;inq[s] = 1;p[s] = 0;a[s] = inf;
        queue<int>Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u = Q.front();

            Q.pop();
            inq[u] = 0;

            for(int i = 0;i < g[u].size(); i++)
            {
                edge& ee = e[g[u][i]];

                //cout<<ee.cap<<"  "<<ee.flow<<"  "<<d[ee.to]<<"  "<<d[u]<<"  "<<ee.cost<<endl;
                if(ee.cap > ee.flow && d[ee.to] > d[u] + ee.cost)
                {
                    //cout<<"zzzz"<<endl;
                    d[ee.to] = d[u] + ee.cost;
                    p[ee.to] = g[u][i];
                    a[ee.to] = min(a[u],ee.cap - ee.flow);
                    if(!inq[ee.to])
                    {
                        Q.push(ee.to);
                        //cout<<"in="<<ee.to<<endl;
                        inq[ee.to] = 1;
                    }
                }
            }
        }

        //cout<<"hahaha"<<endl;
        if(d[t] == inf)
            return false;
        flow += a[t];
        cost += d[t] * a[t];
        int u = t;
        while(u != s){
            e[p[u]].flow += a[t];
            e[p[u]^1].flow -= a[t];
            u = e[p[u]].from;
        }

        return true;
    }
    int minCost(int s,int t)
    {
        int flow = 0,cost = 0;
        while(bellman(s,t,flow,cost));
        return cost;
    }
}solve;

int main(){
    int k,c,d;
    cin>>n>>m>>k>>c>>d;

    for(int i=1;i<=k;i++){
        scanf("%d",&a[i]);
    }

    startPoint=0,endPoint=n*100+1;

    for(int i=1;i<=k;i++){
        solve.add(startPoint,1+(a[i]-1)*100,1,0);
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<100;j++){
            int p=(i-1)*100LL;
            solve.add(p+j,p+j+1,inf,c);
        }
    }

    for(int i=1;i<=100;i++)solve.add(i,endPoint,inf,0);

    while(m--){
        int x,y;
        scanf("%d%d",&x,&y);
        for(int i=1;i<100;i++){
            int p=(x-1)*100+i;
            int pp=(y-1)*100+i+1;
            for(int j=1;j<=50;j++){
                solve.add(p,pp,1,d*(j*j-(j-1)*(j-1))+c);
            }
        }

        for(int i=1;i<100;i++){
            int p=(y-1)*100+i;
            int pp=(x-1)*100+i+1;
            for(int j=1;j<=50;j++){
                solve.add(p,pp,1,d*(j*j-(j-1)*(j-1))+c);
            }
        }

    }

    printf("%d\n",solve.minCost(startPoint,endPoint));
    return 0;

}


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

typedef long long ll;
const int maxn=6000;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
//#define endl '\n'

int n,s,t;
struct edge
{
    int to,cap,cost,rev;
};
vector<edge> G[maxn];
int dist[maxn];
int prevv[maxn],preve[maxn];
int inq[maxn];

void init(int _n,int _s,int _t)
{
    for (int i=0;i<=_n;i++) G[i].clear();
    n=_n;
    s=_s;
    t=_t;
}

void add_edge(int from,int to,int cap,int cost)
{
    G[from].pb({to,cap,cost,sz(G[to])});
    G[to].pb({from,0,-cost,sz(G[from])-1});
}

int min_cost_flow(int f)
{
    int res=0;
    while (f>0)
    {
        fill(dist,dist+n,inff);
        dist[s]=0;
        queue<int> q;
        q.push(s);
        inq[s]=1;
        while (!q.empty())
        {
            int now=q.front();
            q.pop();
            inq[now]=0;
            for (int i=0;i<sz(G[now]);i++)
            {
                edge &e=G[now][i];
                if (e.cap>0&&dist[now]+e.cost<dist[e.to])
                {
                    dist[e.to]=dist[now]+e.cost;
                    prevv[e.to]=now;
                    preve[e.to]=i;
                    if (!inq[e.to]) inq[e.to]=1,q.push(e.to);
                }
            }
        }

        if (dist[t]==inff) return -1;

        int d=f;
        for (int v=t;v!=s;v=prevv[v])
        {
            d=min(d,G[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*dist[t];
        for (int v=t;v!=s;v=prevv[v])
        {
            edge &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
}

int N,M,K,C,D,X,Y;
int A[58];
vector<int> H[57];

int32_t main()
{
    IOS
    cin>>N>>M>>K>>C>>D;
    for (int i=1;i<=K;i++)
    {
        cin>>X;
        A[X]++;
    }
    for (int i=1;i<=M;i++)
    {
        cin>>X>>Y;
        H[X].pb(Y);
        H[Y].pb(X);
    }

    init(5103,0,5101);
    for (int i=2;i<=N;i++) add_edge(0,i,A[i],0);
    for (int i=0;i<=100;i++)
    {
        for (int l=1;l<=N;l++)
        {
            add_edge(50*i+l,50*(i+1)+l,K,0);
            for (int j:H[l])
            {
                for (int k=1;k<=K;k++)
                {
                    add_edge(50*i+l,50*(i+1)+j,1,D*(2*k-1));
                }
            }
        }
    }
    for (int i=1;i<=101;i++) add_edge(50*i+1,5101,K,C*i);
    cout<<min_cost_flow(K)<<endl;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// DON'T USE THESE MACROS DURING ICPC PRACTICE
#define For(i,n) for (int i=0; i<n; i++)
#define FOR(i,a,b) for (int i=a; i<=b; i++)
#define Down(i,n) for (int i=n-1; i>=0; i--)
#define DOWN(i,a,b) for (int i=b; i>=a; i--)

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef complex<ld> pt;
typedef vector<pt> pol;
typedef vector<int> vi;

const char nl = '\n';
const ll INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1e9+7;
const ld EPS = 1e-10;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int T = 99;
const int C = 15;
namespace Flow {
	const int N = 50*T + 2;
	const int M = 2*N*C + N + 7;
	// data structures and helper functions common to all flow routines
	int par[N], first[N], nxt[2*M], ep[2*M], m;
	ll flo[2*M], cap[2*M], cost[2*M];
	void init() { m = 0; memset(first,-1,sizeof first); memset(flo,0,sizeof flo); }
	void add_edge(int a, int b, ll c=1, ll p=0) { //a,b - nodes, c, p - cap, price
		nxt[m] = first[ep[m]=a], first[ep[m]] = m, cap[m] = c, cost[m] =  p, ++m;
		nxt[m] = first[ep[m]=b], first[ep[m]] = m, cap[m] = 0, cost[m] = -p, ++m; }
	// Minimum cost maximum flow, assuming there are no negative cost cycles
	// USAGE: 1) init(); 2) add edges, 3) mcf_pot_init(numV) and 4)
	// ll price=0,flow=0; while (ll df = mcf_update(s, t, price, numV)) flow += df;
	//! for sparse graphs, may help to change Dijkstra from O(N^2) to O(M*lgN)
	int q[N], inq[N];
#define BUBL { \
	t = q[i]; q[i] = q[j]; q[j] = t; \
	t = inq[q[i]]; inq[q[i]] = inq[q[j]]; inq[q[j]] = t; }
	bool vis[N]; ll pot[N], dist[N];
	void mcf_pot_init(int n) { memset(pot, 0, sizeof pot);
		// if all edge costs >= 0, we can safely return before the Bellman-Ford here
		for (int k = 1; k < n; ++k) for (int e = 0; e < m; ++e)
			if (cap[e]) pot[ep[e^1]] = min(pot[ep[e^1]], pot[ep[e]] + cost[e]); }
	ll mcf_update(int s, int t, ll& price, int n) {
		memset(vis, 0, sizeof vis); memset(dist, INF, sizeof dist); dist[s] = 0;
		memset(inq, -1, sizeof inq);
		int qs = 0; inq[q[qs++] = s] = 0;
		while (qs) {
			int u = q[0]; inq[u] = -1;
			q[0] = q[--qs];
			if( qs ) inq[q[0]] = 0;
			for( int i = 0, j = 2*i + 1, t; j < qs; i = j, j = 2*i + 1 ) {
				if( j + 1 < qs && dist[q[j + 1]] < dist[q[j]] ) j++;
				if( dist[q[j]] >= dist[q[i]] ) break;
				BUBL; }
			if (vis[u] == 1) continue; vis[u] = 1;
			for (int e = first[u]; e != -1; e = nxt[e]) { int v = ep[e^1];
				if (flo[e] < cap[e] && dist[v] > dist[u] + pot[u] - pot[v] + cost[e]) {
					dist[v] = dist[u] + pot[u] - pot[v] + cost[e], par[v] = e;
					if( inq[v] < 0 ) { inq[q[qs] = v] = qs; qs++; }
					for( int i = inq[v], j = ( i - 1 )/2, t;
							dist[q[i]] < dist[q[j]]; i = j, j = ( i - 1 )/2 )
						BUBL;
				}
			}
		}
		if (dist[t] >= INF) return 0; ll df = INF;
		for (int e, i = t; i != s; i = ep[e])
			e = par[i], df = min(df, cap[e] - flo[e]);
		for (int e, i = t; i != s; i = ep[e])
			e = par[i], flo[e] += df, flo[e^1] -= df, price += df*cost[e];
		for (int i = 0; i < n; ++i) pot[i] = min(INF, dist[i] + pot[i]);
		return df; }
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << fixed << setprecision(10);

	int n,m,k,c,d;
	cin>>n>>m>>k>>c>>d;

	const int source = 0;
	const int sink = n*T+1;
	Flow::init();

	for(int i=0;i<k;i++) {
		int a;
		cin>>a;
		Flow::add_edge(source, a, 1, 0);
	}
	for(int t=0;t<T;t++) {
		Flow::add_edge(1+t*n, sink, INF, 0);
		if(t>0) {
			for(int i=1;i<=n;i++) {
				Flow::add_edge(i+(t-1)*n, i+t*n, INF, c);
			}
		}
	}

	for(int i=0;i<m;i++) {
		int a, b;
		cin >> a >> b;
		for(int j=0;j<C;j++) {
			int cost = (j+1)*(j+1) - j*j;
			for(int t=1;t<T;t++) {
				Flow::add_edge(a+(t-1)*n, b+t*n, 1, cost*d + c);
				Flow::add_edge(b+(t-1)*n, a+t*n, 1, cost*d + c);
			}
		}
	}

	Flow::mcf_pot_init(Flow::N);
	ll price = 0;
	ll flow = 0;
	while (ll df = Flow::mcf_update(source, sink, price, Flow::N)) flow += df;
	assert(flow==k);
	cout << price << nl;

	return 0;
}