# [简单题] Project Euler 603 Substring sums of prime concatenations

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

const int P=1e9+7;

const int maxn=1e6;
ll n,K=1e12,NN;
int num,prime[maxn+5];
bool vst[(maxn+5)*20];
void Pre(int n){
for (int i=2;i<=n;i++){
if (!vst[i]) prime[++num]=i;
for (int j=1;j<=num && (ll)i*prime[j]<=n;j++){
vst[i*prime[j]]=1;
if (i%prime[j]==0) break;
}
}
}

int S[(maxn+5)*10];

ll Ans=0;

inline ll Pow(ll A,ll B){
B=(B%(P-1)+P-1)%(P-1); A%=P;
ll ret=1;
for (;B;B>>=1,A=A*A%P)
if (B&1)
ret=ret*A%P;
return ret;
}
inline ll Inv(ll A){
return Pow(A,P-2);
}

inline void Calc(ll I,ll v){
ll S;
S=I*Pow(10,NN+1+n-I)%P+P-((K-1)*n+I)%P*Pow(10,NN+1-(K-1)*n-I)%P+n*Pow(10,NN+1-I)%P*(Pow(10,-n*(K-1))+P-1)%P*Inv(Pow(10,-n)+P-1)%P;
S%=P;
(S*=Inv(Pow(10,n)+P-1))%=P;
(S+=P-(I+(K-1)*n+I)%P*(K%P)%P*Inv(2)%P)%=P;
S=S*Inv(9)%P;
(Ans+=S*v%P)%=P;
}

inline void Brute(){
ll ret=0;
for (int i=1;i<=n*K;i++){
ll t=0;
for (int j=i;j<=n*K;j++){
t=(t*10+S[(j-1)%n+1])%P;
(ret+=t)%=P;
}
}
printf("%lld\n",ret);
}

int main(){
Pre(maxn*20);
for (int i=1;i<=maxn;i++){
int a[11],t=prime[i]; *a=0;
while (t) a[++*a]=t%10,t/=10;
for (int j=*a;j;j--)
S[++n]=a[j];
}
NN=n*K;
for (int i=1;i<=n;i++)
Calc(i,S[i]);
printf("%lld\n",Ans);
//Brute();
return 0;
}