Why answer should be in modulo 10^9+7

Why we have to answer the result in modulo of 10^9+7. The reason behind this is, time constraints. When we deal with very large numbers though implementing using efficient algorithms results in TLE or outputs us some garbage value.

Why modulo

  • To prevent overflow issue. For instance if a 64 bit variable ‘A’ with the value of 2^63( as the highest positive value of 64 bit is 2^63 -1) is multiplied with 64 bit variable 'B' with the value 2^63 , instead of TLE or exception it outputs us some garbage value (as most significant digits are dropped off)

Why 10^9+7

  • 10^9+7 is large enough to fit in the largest integer data type

  • 10^9+7 is also a prime number, usually when we take mod of a number by Prime the results are generally distinct when compared with taking mod of a number by non-prime.