这常数卡得也太过头了吧?
#include<cstdio>
#define int long long
using namespace std;
int mul[5000005],ans,n,p,k,pre[5000005],last[5000005];
void exgcd(int a,int b,int &x,int &y)
{
if (b==0)
{
x=1,y=0;
return ;
}
exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
inline int read()
{
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
}
signed main()
{
n=read();p=read();k=read();
mul[0]=1;
for (int i=1;i<=n;i++)
mul[i]=mul[i-1]*k%p;
int s=1,x,y;pre[0]=1;last[n+1]=1;
for (int i=1;i<=n;i++)
{
last[i]=read();
s*=last[i];s%=p;
pre[i]=pre[i-1]*last[i]%p;
}
for (int i=n;i>=1;i--)
last[i]=last[i+1]*last[i]%p;
exgcd(s,p,x,y);
x=(x%p+p)%p;
for (int i=1;i<=n;i++)
ans=(ans%p+mul[i]%p*pre[i-1]%p*last[i+1]%p)%p;
printf("%lld",ans*(x%p+p)%p);
return 0;
}