Let me trace powerMod(3,13,1000):
Binary of 13: 11012. Process bits right to left.
1. n=13 (odd), result=1×3=3, x=32mod1000=9, n=6
2. n=6 (even), result=3, x=92mod1000=81, n=3
3. n=3 (odd), result=3×81=243, x=812mod1000=561, n=1
4. n=1 (odd), result=243×561=136323mod1000=323
Final answer: 323. You computed 313mod1000 in just 4 steps instead of 13 multiplications.