  We confirm several conjectures of Sun involving quadratic residues modulo odd
primes. For any prime $p\equiv 1\pmod 4$ and integer $a\not\equiv0\pmod p$, we
prove that \begin{align*}&(-1)^{|\{1\le k<\frac p4:\ (\frac
kp)=-1\}|}\prod_{1\le j<k\le(p-1)/2}(e^{2\pi iaj^2/p}+e^{2\pi iak^2/p})
\\=&\begin{cases}1&\text{if}\ p\equiv1\pmod 8,\\\left(\frac
ap\right)\varepsilon_p^{-(\frac ap)h(p)}&\text{if}\ p\equiv5\pmod8,\end{cases}
\end{align*} and that \begin{align*}&\left|\left\{(j,k):\ 1\le
j<k\le\frac{p-1}2\ \&\ \{aj^2\}_p>\{ak^2\}_p\right\}\right|
\\&+\left|\left\{(j,k):\ 1\le j<k\le\frac{p-1}2\ \&\ \{ak^2-aj^2\}_p>\frac
p2\right\}\right| \\\equiv&\left|\left\{1\le k<\frac p4:\ \left(\frac
kp\right)=\left(\frac ap\right)\right\}\right|\pmod2. \end{align*} where
$(\frac{a}p)$ is the Legendre symbol, $\varepsilon_p$ and $h(p)$ are the
fundamental unit and the class number of the real quadratic field $\mathbb
Q(\sqrt p)$ respectively, and $\{x\}_p$ is the least nonnegative residue of an
integer $x$ modulo $p$. Also, for any prime $p\equiv3\pmod4$ and $\delta=1,2$,
we determine $$(-1)^{\left|\left\{(j,k): \ 1\le j<k\le(p-1)/2\ \text{and}\
\{\delta T_j\}_p>\{\delta T_k\}_p\right\}\right|},$$ where $T_m$ denotes the
triangular number $m(m+1)/2$.
