Why does this equality between expectations hold? Why does this equality between expectations hold (second equality)?

 A: We have that
$$
\biggl[\sum_{i=1}^na_i\biggr]^2=\sum_{i=1}^na_i^2+2\sum_{i=2}^n\sum_{j=1}^{i-1}a_ia_j
$$
for real numbers $a_1,\ldots,a_n$ and
$$
\operatorname E\biggl[\sum_{k=1}^nX_k\biggr]=\sum_{k=1}^n\operatorname EX_k
$$
for random variables $X_1,\ldots,X_n$.
A: $$\begin{align}
\tag{1}\left(\sum_{i=1}^n r_i\right)^2 & = (r_1+\cdots+r_n)(r_1+\cdots+r_n)
\\[0ex]\tag{2} & = r_1(r_1+\cdots+r_n)+r_2(r_1+\cdots+r_n)+\cdots+r_n(r_1+\cdots+r_n)
\\[2ex]\tag{3} & = (r_1^2 +\cdots+r_n^2) +r_1(r_2+\cdots+ r_n)+r_2(r_1+r_3+\cdots+r_n)+\cdots+r_n(r_1+\cdots+r_{n-1})
\\[2ex]\tag{4} & = (r_1^2 +\cdots+r_n^2) + 2r_1(r_2+\cdots+ r_n)+2r_2(r_3+\cdots+r_n)+\cdots+2r_{n-1}r_n
\\[1ex]\tag{5} & = \sum_{i=1}^n (r_i^2) + 2r_1(r_2+\cdots+ r_n)+2r_2(r_3+\cdots+r_n)+\cdots+2r_{n-1}r_n
\\[1ex]\tag{6} & = \sum_{i=1}^n (r_i^2) + 2r_1\sum_{j=2}^n (r_j)+2r_2\sum_{j=3}^n (r_j)+\cdots+2r_{n-1}\sum_{j=n}^n (r_j)
\\[1ex]\tag{7} & = \sum_{i=1}^n (r_i^2) + 2\sum_{i=1}^{n-1} \left(r_i\sum_{j=i+1}^n (r_j)\right)
\\[1ex]\tag{8} & = \sum_{i=1}^n (r_i^2) + 2\sum_{i=1}^{n-1} \sum_{j=i+1}^n (r_i r_j)
\\[3ex]\tag{9} \mathsf E\left[\left(\sum_{i=1}^n r_i\right)^2\right] & = \mathsf E\left[\sum_{i=1}^n (r_i^2) + 2\sum_{i=1}^{n-1} \sum_{j=i+1}^n (r_i r_j)\right]
\\[1ex]\tag{10} & = \mathsf E\left[\sum_{i=1}^n (r_i^2)\right] + 2 \mathsf E\left[\sum_{i=1}^{n-1} \sum_{j=i+1}^n (r_i r_j)\right]
\end{align}$$
