S=(a-1)+(a^2-2)+.+(a^n-n)
=a+a^2+a^3+.+a^n-(1+2+3+.+n)
=a(1-a^n)/(1-a)-(1+an)*n/2
=(a-a^n+1)/(1-a)-(1+an)*n/2