There is an algorithm for that, it is called modular exponentiation.

It is done using a decomposition in base 2, which is very convenient for computer programming, but can still be done using a calculator.

The idea to compute is to decompose in base 2:

.

where the are 0 or 1. Then we have , and the factors can be computed recursively:

- You know since .

- Compute with your calculator. This gives you (no computation needed since ).

- Compute . You have hence .

- Compute . You have hence .

- ...

As a summary, you compute , ,... . This gives you ,... , , and you multiply them: (to avoid dealing with too big numbers, you can compute the modulo after each multiplication).

You can find a more algorithmic presentation here, with the advantage that the decomposition in base 2 is done along with the other computations.