def cumsum(x,d=1): for i in range(d,len(x)): x[i]+=x[i-d] N,P=map(int,input().split()) ans=0 f=[0,0] fact=1 for i in range(2,N+1): f.append((pow(N,N-i,P)*fact)%P) fact*=N-i fact%=P dist=[0]*N cum=0 pat=0 for i in range(N-2,0,-1): cum+=f[i+2] pat+=cum cum%=P pat%=P dist[i]=pat backet=150 pats=[0]*N*2 det=[[0]*N*2 for i in range(backet+1)] for i in range(1,N+1): for j in range(N+1): if i*j>=N: break if j==0: pats[1]+=N-i+1 pats[i*(j+1)]-=N-i+1 continue if j>backet: cnt=0 for k in range(i*j+1,i*(j+1),j): pats[k]+=1 cnt+=1 pats[i*(j+1)]-=cnt else: det[j][i*j+1]+=1 cnt=(i*(j+1)-2)//j-i+1 det[j][min(N*2-1,i*j+1+cnt*j)]-=1 pats[i*(j+1)]-=cnt for i in range(1,backet+1): cumsum(det[i],i) for j in range(N*2): pats[j]+=det[i][j] cumsum(pats) for i in range(N-1): pats[i]=N*(N+1)//2-pats[i] ans+=pats[i]*dist[i] ans%=P cumsum(f) div=[[] for i in range(N+1)] cum=[[0] for i in range(N+1)] for i in range(1,N+1): for j in range(i,N+1,i): div[j].append(i) cum[i].append(cum[i][-1]+f[j]) for i in range(1,N+1): gcd=[0]*len(div[i]) for j in range(len(div[i])-1,-1,-1): gcd[j]+=N//div[i][j] for k in range(j): if div[i][j]%div[i][k]==0: gcd[k]-=gcd[j] pl=cum[div[i][j]][-1]-cum[div[i][j]][i//div[i][j]] plcnt=len(cum[div[i][j]])-1-i//div[i][j] mi=cum[div[i][j]][-1]-cum[div[i][j]][i//div[i][j]-1]+f[i-1]*(i//div[i][j]-1) micnt=plcnt+1+i//div[i][j]-1 ans+=(pl+f[-1]*(micnt-plcnt)-mi)*gcd[j] if j==0: ans+=(pl+f[-1]*(micnt-plcnt)-mi)*N*(N-1)//2 ans%=P print((ans*N*(N-1))%P)