/*
k<=1e18个字母 构造L<=1e18 长度的字符串 以及s位置的字符是w
问 没有长度>1的回文字串 的方案数
1.若没有限制 则k*(k-1)*(k-2)^len-2
2.有限制 (k-1)*(k-2)^len-2
eg: k=4 l=5 X*3*2*2*2 3*X*2*2*2 .....
*/
/*
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<queue>
#include<vector>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e1 +10;
const int mod=998244353;
const int inf=0x3f3f3f3f;
ll k,l,p,s,w,ans=1;
ll ksm(ll a,ll b)
{
ll res=1;
while(b)
{
if(b%2==1) res=(res*a)%p;
a=a*a%p,b>>=1;
}
return res%p;
}
int main()
{
ios::sync_with_stdio(false);
cin>>k>>l>>p>>s>>w;
k%=p;
if(l==1)
{
if(s) cout<<"1\n";
else cout<<k<<'\n';
return 0;
}
if(s) ans=ans*(k-1)%p;
else ans=ans*k*(k-1)%p;
ans=(ans*ksm(k-2,l-2))%p;
cout<<ans<<'\n';
return 0;
}