In the following system of congruences, $n_1$ and $n_2$ are relatively prime.
\begin{align} \newcommand{\mod}{\text{mod }} a &\equiv a_1 \; (\mod n_1) \\ a &\equiv a_2 \; (\mod n_2) \\ \end{align}
Using the Chinese Remainder System described in [CLRS: Introduction to Algorithms (Theorem 31.27, Page 951, the 3rd edition)], we have
$$m_1 = n_2, m_2 = n_1$$ \begin{align} c_1 = m_1(m_1^{-1} \;\mod n_1) = n_2 (n_2^{-1} \;\mod n_1) \\ c_2 = m_2(m_2^{-1} \;\mod n_2) = n_1 (n_1^{-1} \;\mod n_2) \end{align}
Then, we have ($n = n_1 n_2$ in the following)
\begin{align} a &= (a_1 c_1 + a_2 c_2) \; (\mod n) \\ &= \left(a_1 n_2 (n_2^{-1} \;\mod n_1) + a_2 n_1 (n_1^{-1} \;\mod n_2)\right) \; (\mod n) \end{align}
Another method is to solve the congruences directly as follows.
$$a \equiv a_1 \; (\mod n_1) \Rightarrow \exists x \in \mathbb{Z}, a = n_1 x + a_1$$
Substitute $a$ into the second congruence, we get $$n_1 x \equiv a_2 - a_1 \; (\mod n_2)$$
Solving the congruence above, we get $$x = (a_2 - a_1) (n_1^{-1} \; \mod n_2)$$
Therefore, \begin{align} a &= n_1 x + a_1 \\ &= n_1 \left( (a_2 - a_1) (n_1^{-1} \; \mod n_2) \right) + a_1 \\ &= a_2 n_1 (n_1^{-1} \;\mod n_2) - a_1 n_1 (n_1^{-1} \;\mod n_2) + a_1 \end{align}
However, the result is not the same as the one obtained by applying the Chinese Remainder Theorem. What is wrong with it?