Example 3.7 Use $m(m \geqslant 2)$ colors to paint a $1 \times n(n \geqslant 2)$ chessboard, each cell is painted with one color, adjacent cells have different colors, and the first and last cells also have different colors, find the number of different coloring methods.
Solution: Let $m(m \geqslant 2)$ be fixed, and let $a_{n}$ denote the number of different coloring methods. It is clear that $a_{2}=m(m-1)$. Since using $m(m \geqslant 2)$ colors to paint a $1 \times n(n \geqslant 3)$ chessboard (each cell is painted one color), such that adjacent cells have different colors, there are $m(m-1)^{n-1}$ ways in total, among which the first and last cells have different colors in $a_{n}$ ways, and the first and last cells have the same color in $a_{n-1}$ ways (because the first $n-1$ cells can be painted first, and then the last cell is made to have the same color as the first cell). By the principle of addition, we have
$$
a_{n}+a_{n-1}=m(m-1)^{n-1} \quad(n \geqslant 3),
$$

Therefore,
$$
\begin{aligned}
a_{n}= & -a_{n-1}+m(m-1)^{n-1}, \\
(-1)^{n} a_{n}= & (-1)^{n-1} a_{n-1}-m(1-m)^{n-1} \\
= & (-1)^{n-2} a_{n-2}-m(1-m)^{n-2}-m(1-m)^{n-1} \\
= & \cdots \\
= & (-1)^{2} a_{2}-m(1-m)^{2}-m(1-m)^{3}- \\
& \cdots-m(1-m)^{n-1} \\
= & -m(1-m)-m(1-m)^{2}-\cdots-m(1-m)^{n-1} \\
= & -m\left[(1-m)+(1-m)^{2}+\cdots+(1-m)^{n-1}\right] \\
= & -m \cdot \frac{(1-m)-(1-m)^{n}}{1-(1-m)} \\
= & (1-m)^{n}+(m-1), \\
a_{n}= & (m-1)^{n}+(-1)^{n}(m-1) \quad(n \geqslant 3),
\end{aligned}
$$

It is clear that the above formula still holds when $n=2$, so
$$
a_{n}=(m-1)^{n}+(-1)^{n}(m-1) \quad(n \geqslant 2) .
$$