The reason we have to do this (+mod) trick is that many languages return (a % b) to be zero or the same sign as a, instead of b. Fortunately, python does it right, so you can safely write (Q-P) % mod .
your modular() function has exponential complexity.
what happen if Q is less than P then ans will become negative…
if u think Q never less than P than u r wrong because u r taking mod so it can happen so to avoid thid possibility be add mod in Q than subtract P from this…than take mode print ans u will get AC…
#prasadram126 …u r using int data type it is not enough to store big value like 10^9 so use long int or long long int …u will get AC for first subtask but u will TLE for remaining to get full point u have to use merge sort…here i have modified ur solution u can access from here CodeChef: Practical coding for everyone…
may be Q less than P in that case ans will become negative which not right…
if u think how it is possible …because we are taking mod in whole process it may become less than p think if Q= mod+1 at any stage because u take mod at every stage it will become 1 and P may be 2 than ans will become negative…hope u understood…