#include<bits/stdc++.h>
const int maxn=100000,mod=1e9+7;
inline const void read(long long &a)
{
a=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
{
a=(a<<1)+(a<<3)+c-'0';
c=getchar();
}
}
inline const void write(long long a)
{
if(a>9)write(a/10);
putchar(a%10+'0');
}
long long n,a[maxn],A,B,C,ans=0;
int main()
{
read(n);read(a[1]);read(A);read(B);read(C);
for(int i=2;i<=n;i++)a[i]=(a[i-1]*A+B)%C;
for(int i=1;i<=n;i++)
{
long long p[maxn];
int l=0,r;
for(int j=i;j<=n;j++)
{
p[++l]=a[j];r=l;
while(r>1&&p[r]>p[r-1]){std::swap(p[r],p[r-1]);--r;}
for(int k=1;k<=l;k++)ans=(ans+p[k]*(l-k+1))%mod;
}
}
write(ans);
return 0;
}