12. (16 points) Find the number of permutations $a_{1}, a_{2}, \cdots, a_{n}$ of $1,2, \cdots, n$ such that $\left|a_{k}-k\right| \geqslant \frac{n-1}{2}$ for all positive integers $k=1,2, \cdots, n$.
12. First consider the case where $n$ is even.

Let $n=2 m$ ($m$ is a positive integer).
For $k=1,2, \cdots, 2 m$, we have
$$
\left|a_{k}-k\right| \geqslant \frac{n-1}{2}=m-\frac{1}{2}
$$

and since $a_{k}$ is a positive integer, we know
$a_{k} \geqslant m+k$ or $a_{k} \leqslant k-m$.
Noting that $1 \leqslant a_{k} \leqslant n=2 m$.
Thus, $m+k \leqslant a_{k} \leqslant 2 m$ for $1 \leqslant k \leqslant m$,
and $1 \leqslant a_{k} \leqslant k-m$ for $m+1 \leqslant k \leqslant 2 m$,

where the choices for $a_{m}, a_{m-1}, \cdots, a_{1}$ are unique.
In fact, noting that $a_{1}, a_{2}, \cdots, a_{m}$ are all distinct, and repeatedly using equation (2), we know $a_{m}$ can only be $2 m$.
Then, $a_{m-1}$ can only be $2 m-1$. Continuing this, we get
$a_{m-t}=2 m-t$ for $0 \leqslant t \leqslant m-1$.
Similarly, by equation (3), the choices for $a_{m+1}, a_{m+2}, \cdots, a_{2 m}$ are also unique.

Therefore, when $n=2 m$, there is exactly 1 permutation that meets the condition.
Next, consider the case where $n$ is an odd number greater than 1.
Let $n=2 m+1$ ($m$ is a positive integer).
Similarly to the even case, we know
$k+m \leqslant a_{k} \leqslant 2 m+1$ for $1 \leqslant k \leqslant m$,
and $1 \leqslant a_{k} \leqslant k-m$ for $m+2 \leqslant k \leqslant 2 m+1$,

and $a_{m+1} \in\{1,2 m+1\}$.
Without loss of generality, assume $a_{m+1}=1$.
First, we know the choices for $a_{m+2}, a_{m+3}, \cdots, a_{2 m+1}$ are unique.
In fact, noting that $a_{m+1}, a_{m+2}, \cdots, a_{2 m+1}$ are all distinct, and repeatedly using equation (5), we know $a_{m+2}$ can only be 2, $a_{m+3}$ can only be 3. Continuing this, we get
$$
a_{m+1}=t \quad (1 \leqslant t \leqslant m+1) .
$$

For the remaining numbers $a_{1}, a_{2}, \cdots, a_{m}$, we will prove there are $2^{m-1}$ ways to choose them.

In fact, by equation (4), we know $a_{m} \in\{2 m, 2 m+1\}$, i.e., $a_{m}$ has two possible values.

For each $t (2 \leqslant t \leqslant m-1)$, when $a_{m}, a_{m-1}, \cdots, a_{t+1}$ (these numbers are all at least $t+1+m$) are determined, using equation (4) again, we know $a_{t}$ is chosen from the set
$$
\{t+m, t+m+1, \cdots, 2 m+1\},
$$

and cannot take the values of $a_{m}, a_{m-1}, \cdots, a_{t+1}$, so the number of ways to choose $a_{t}$ is
$$
[2 m+1-(t+m)+1]-(m-t)=2 \text {. }
$$

When $a_{m}, a_{m-1}, \cdots, a_{2}$ are all determined, the value of $a_{1}$ is uniquely determined. Therefore, by the multiplication principle, the number of ways to choose $a_{m}, a_{m-1}, \cdots, a_{1}$ is $2^{m-1}$.

The above shows that, among the permutations that meet the condition, there are $2^{m-1}$ permutations that satisfy $a_{m+1}=1$. Similarly, there are $2^{m-1}$ permutations that satisfy $a_{m+1}=2 m+1$.

Therefore, when $n=2 m+1$, there are $2^{m-1}+2^{m-1}=2^{m}$ permutations that meet the condition.

In summary, when $n$ is even, there is exactly 1 permutation that meets the condition; when $n$ is odd, there are $2^{\frac{n-1}{2}}$ permutations that meet the condition.