  Let $p$ be an odd prime. In this paper we investigate quadratic residues
modulo $p$ and related permutations, congruences and identities. If
$a_1<\ldots<a_{(p-1)/2}$ are all the quadratic residues modulo $p$ among
$1,\ldots,p-1$, then the list $\{1^2\}_p,\ldots,\{((p-1)/2)^2\}_p$ (with
$\{k\}_p$ the least nonnegative residue of $k$ modulo $p$) is a permutation of
$a_1,\ldots,a_{(p-1)/2}$, and we show that the sign of this permutation is $1$
or $(-1)^{(h(-p)+1)/2}$ according as $p\equiv3\pmod 8$ or $p\equiv7\pmod 8$,
where $h(-p)$ is the class number of the imaginary quadratic field $\mathbb
Q(\sqrt{-p})$. To achieve this, we evaluate the product $\prod_{1\le
j<k\le(p-1)/2}(\cot\pi j^2/p-\cot\pi k^2/p)$ via Dirichlet's class number
formula and Galois theory. We also obtain some new identities for the sine and
cosine functions; for example, we determine the exact value of $$\prod_{1\le
j<k\le p-1}\cos\pi\frac{aj^2+bjk+ck^2}p$$ for any $a,b,c\in\mathbb Z$ with
$ac(a+b+c)\not\equiv0\pmod p$.
