fork(2) download
  1. n = int(input())
  2. a = list(map(int,input().split()))
  3. b=a
  4. t = int(input())
  5. primelist = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
  6. pfreq=[[0 for i in range(25) ]for j in range(n)]
  7. def modular_pow(base, exponent, modulus): # algorithm understood and applied from wikipedia
  8. result = 1
  9. while exponent > 0:
  10. if (exponent % 2 == 1):
  11. result=(result*base)%modulus
  12. exponent = exponent>>1
  13. base = (base * base)%modulus
  14. return result
  15.  
  16. while t>0:
  17. t-=1
  18. c = list(map(int,input().split()))
  19. for i in range(0,n):
  20. for prime in primelist:
  21. if a[i]%prime==0:
  22. while a[i]>0:
  23. ind = primelist.index(prime)
  24. pfreq[i][ind]+=1
  25. a[i] = a[i]//prime
  26. if a[i]%prime==0:
  27. continue
  28. else:
  29. a[i]=b[i]
  30. break
  31. rem=1
  32. for i in range(0,25):
  33. exp=0
  34. for j in range(c[0]-1,c[1]):
  35. exp+=pfreq[j][i]
  36. #rem = (rem*pow(primelist[i],exp,c[2]))%c[2]
  37. rem*=modular_pow(primelist[i],exp,c[2])
  38. rem = rem%c[2]
  39. print(rem)
  40.  
Success #stdin #stdout 0.16s 10256KB
stdin
5
1 2 3 4 5
4
1 2 3
2 3 4
1 1 1
1 5 1000000000
stdout
2
2
0
120