Math Fundamentals18 sections · 814 units
Open in Course

Why Mod in Contests

(Overflow prevention)

Contest problems often ask for the answer mod 10^9 + 7. Why? Because the real answer might be enormous, like 2^1000, which no data type can hold.

By taking mod at each step, you keep numbers under 10^9 + 7. This fits in a 32-bit int and prevents overflow. The modular properties guarantee you get the correct final remainder.

10^9 + 7 is chosen because it is prime and close to 10^9, which is near the 32-bit int limit.