Yup, the usual algorithm is exponentiation by squaring with modular reduction after each step, and I imagine better ones exist. It's a native command in Python if I remember correctly, so it's a common operation. Here's a Wikipedia section discussing it. You can write a recursive version of this algorithm extremely quickly, eg. using the pseudocode in the section following the one I linked and reducing the result each time before returning.