If you want to learn more details about algorithms, please check:
For Chinese Remainder Theorem: https://en.wikipedia.org/wiki/Chinese_remainder_theorem.
For Forbidden Position Permutations: http://homepage.divms.uiowa.edu/~idarcy/COURSES/150/6_4.pdf.