some modular arithmetic stuff:
modpow
modinverse
extended euclidean algorithm