Q=2000000000
T=6308948
mod=50515093
S=[0]*T
S[0]=(290797*290797)%mod
for i in range(1,T):
S[i]=(S[i-1]*S[i-1])%mod
res=Q*(Q+1)//2
ans=0
r=Q%T
m=2*T+r
L=[0]*m
R=[0]*m
st=[]
for i in range(m):
while st and S[i%T] < S[st[-1]%T]:
st.pop()
L[i]=0 if not st else st[-1]+1
st.append(i)
st=[]
for i in range(m-1,-1,-1):
while st and S[i%T] < S[st[-1]%T]:
st.pop()
R[i]=m-1 if not st else st[-1]-1
st.append(i)
for i in range(T):
tmp=(i-L[i]+1)*(R[i]-i+1)
ans+=tmp*S[i]
res-=tmp
j=2*T+r-1-i
tmp=(j-L[j]+1)*(R[j]-j+1)
ans+=tmp*S[j%T]
res-=tmp
tmp=(i+T-L[i+T]+1)*(R[i]-i+1)
c=Q//T-2+(i < r)
ans+=tmp*S[i]*c
res-=tmp*c
ans+=res*min(S)
print ans