My Code : ideone.com/nOiXDY
# your code goes here
import sys
t = input()
while t:
t -= 1
n,p = map(int , raw_input().split() )
x = pow(p,n-1,1000000007)
z = 0
for i in range(0,n):
z = z + pow(p,i,1000000007)
z = z%1000000007
#z = (1 - pow(p,n) ) / (1-p)
print z%1000000007,x
sys.exit(0)
My code work good on ideone , but gives NZEC on spoj , any ideas ? , thanks