#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pr;
const double pi=acos(-1);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define Rep(i,u) for(int i=head[u];i;i=Next[i])
#define clr(a) memset(a,0,sizeof a)
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
ld eps=1e-9;
ll pp=1000000007;
ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
ll read(){
ll ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
//head
#define N 1100000
pr a[N];
int c1[N],c2[N],n;
ll A,B,C;
int lowbit(int x){return x&(-x);}
void add(int *c,int x,int w){
c[0]+=w;
if(c[0]>=pp)c[0]-=pp;
for(;x<=n;x+=lowbit(x)){
c[x]=c[x]+w;
if(c[x]>=pp)c[x]-=pp;
}
}
int find(int *c,int x){
int ans=0;
for(;x;x-=lowbit(x)){
ans+=c[x];
if(ans>=pp)ans-=pp;
}
return ans;
}
int main(){
cin>>n>>a[1].fi>>A>>B>>C;
a[1].sc=1;
rep(i,2,n){
a[i].sc=i;
a[i].fi=(a[i-1].fi*A+B)%C;
}
sort(a+1,a+n+1);
ll ans=0;
rep(i,1,n){
int t1=find(c1,a[i].sc);
int t2=c2[0]-find(c2,a[i].sc-1);
t2=(t2+pp)%pp;
int t3=(1ll*t1*(n-a[i].sc+1)+1ll*t2*a[i].sc)%pp;
t3=(t3+1ll*a[i].sc*(n-a[i].sc+1))%pp;
ans=(ans+1ll*t3*a[i].fi)%pp;
add(c1,a[i].sc,a[i].sc);
add(c2,a[i].sc,n-a[i].sc+1);
}
cout<<ans<<endl;
return 0;
}