0PricingLogin
Competitive Programming Academy · Lesson

Work Modulo a Prime

Add, multiply, and stay inside the mod.

Why Take the Modulo

Contest answers can grow huge, way past any integer type elsewhere. So problems ask for the result modulo a big prime to keep numbers small. 🔢

Meet the Famous Mod

The prime you will see most is 1000000007, often written 1e9+7. It is just big enough to dodge collisions and small enough to fit in 64 bits.

MOD = 1000000007

All lessons in this course

  1. Work Modulo a Prime
  2. Fast Modular Exponentiation
  3. Modular Inverse via Fermat
  4. nCr with Precomputed Factorials
← Back to Competitive Programming Academy