Modular inverse using Fermat's little theorem.
int modInverse(int a, int mod) { return modExp(a, mod - 2, mod); }