Can someone explain why this works?

5^101 mod 124 = 5^(101 mod 3) mod 124 = 5^2 mod 124 = 25

How did they know to choose 3 in the problem above? If given a problem like this, I don't see how I would deduce that I need to take 101 mod 3 to make the problem easier.

Thanks

Edit: Is there a general formula for this? Can I use this in any problem with a large exponent?