Try using modulus and Euler's Theorem (if you're dealing with the prime a). Or you could try finding the order of a mod 10000 and using modulo arithmetic from there. That should reduce the problem to a much simpler one - at least, you won't have to multiply a out 1000 times.