You have the idea for a modulus of 2 or 3.
There are lots of different methods for finding the solution for general $n$.
THE INDIRECT APPROACH
Suppose we take some number $a$, and we want $a^7 (\bmod \;n)$. Then we find the prime divisors of $n$. For example, if we want $a^7 (\bmod \;21)$, we find $a^7 (\bmod \;3)$ and $a^7 (\bmod \;7)$. Then we find which number is equivalent modulo 3 and 7.
In the case where the divisors repeat, for example, modulo $4=2^2$, we would need to find the value modulo 4. In this case, we can use the direct approach.
THE DIRECT APPROACH
There is a direct approach, where we can speed up calculations mod $n$. For example, suppose we take some number $a$, and we want $a^7 (\bmod \;n)$. The idea is that if instead of 7 we had 700 for the exponent, calculations can be done very quickly. So, We take seven and rewrite it as binary: 7 is 111 in binary, or 1*4 + 1*2 + 1*1. Then we take powers of seven. We start by calculating $a^1 (\bmod \; n)$. This gives us the modulated value of $a (\bmod \; n)$. Then we double this value and take the remainder, to get $a^2 (\bmod n)$. We double it again to get $a^4 (\bmod \; n)$, and so on. We keep doubling the value until we reach a high enough power. Then we simply multiply the previous results together. In other words, $a^7 = (a^4)(a^2)(a^1)$. So by multiplying the results together we arrive at the correct result:
$$a^7 (\bmod \; n) \equiv (a^4)(a^2)(a^1) (\bmod \; n)$$
Just to hammer the idea home, suppose we want to find $a^{13} (\bmod n)$. 13 in binary is $1*8 + 1*4 + 1*1$. So we know we want the powers 8, 4, and 1. So we take
$$a^{13} (\bmod \; n) \equiv (a^8)(a^4)(a^1) (\bmod \; n)$$
OTHER METHODS
Other methods do exist, but the above two are probably the most basic.