How is quantum function $f(x) = a^x \mod N$ constructed in Shor's algorithm?
This paper by Pavlidis and Gizopoulos walks through one possible construction of the circuit you're looking for and also provides references to other literature on the subject:

"Fast Quantum Modular Exponentiation Architecture for Shor's Factorization Algorithm". Archimedes Pavlidis, Dimitris Gizopoulos.  http://arxiv.org/abs/1207.0511