7. For a polygon with $n$ sides $(n \in \mathbf{N}, n \geqslant 3)$, the sides are denoted by $a_{1}$, $a_{2}$, $a_{3}, \cdots, a_{n}$, and they are connected in sequence. If each side is colored with one of three colors: red, yellow, or blue (each side is colored, and adjacent sides must be different colors), the total number of coloring methods is $\qquad$
7. $2^{n}+2 \cdot(-1)^{n}$.

Solution: The first edge $a_{1}$ has three coloring methods, and each $a_{i}(i \geqslant 2)$ has two coloring methods. First, without considering the case where $n$ edges form a closed loop, imagine $a_{1}$ and $a_{n}$ are disconnected, at this point there are $3 \times 2^{n-1}$ coloring methods.

Let the number of coloring methods where $a_{1}$ and $a_{n}$ have different colors be $S(n)$, and the number of coloring methods where $a_{1}$ and $a_{n}$ have the same color can be considered as $a_{1}, a_{n}$ being "connected" as one segment, in this case there are $S(n-1)$ coloring methods. Therefore, we have: $S(n)+S(n-1)=3 \cdot 2^{n-1}$,

Thus, $S(n)-2^{n}=-\left[S(n-1)-2^{n-1}\right]$, which means the sequence $\left\{S(n)-2^{n}\right\}$ is a geometric sequence with the first term $S(3)-2^{3}$ and common ratio -1, and $S(n)-2^{n}$ is the $(n-3)$-th term of this sequence. It is easy to find that $S(3)=6$.
$$
\text { Hence } S(n)=2^{n}+2 \cdot(-1)^{n}(n \in \mathbf{N}, n \geqslant 3) \text {. }
$$