I'm doing an exercise from the book Probability and Random Processes by Grimmett and Stirzaker, and I have difficulty understanding one of the solutions from the solution manual.
I don't understand the expansion of the Expectation that is the last step on page 162. (See image below). I think it may be related to this question, which would explain the first term. $E(I_1^2)$ would equal $E(I_1)$ since $I_1$ is an indicator function. However, I do not understand the other terms.
Thanks in advance for any help.

This line is coming from expanding the square of a product. Note that $$\mathbb E \left\{ \left( \sum_{j=1}^{n-1} I_j \right)^2 \right\} = \mathbb E \left[ \sum_{j=1}^{n-1} I_j^2 + 2\sum_{j <k} I_j I_k \right]$$ and as you noted, $I_j^2 = I_j$, which gives the first term. For the second, we are summing over all pairs $\{j, k\}$ of distinct numbers in $\{1, \dots, n-1\}$. Let's break this apart into two cases: first, we handle the pairs that are one apart from one another (e.g. $\{1, 2\}, \{4, 5\}$). Here, we get $$\mathbb E \left[\dots + 2 \sum_{j < k,\ |j-k| = 1} I_j I_k + \dots \right]$$ and there are $(n-1) - 1 = n-2$ such terms, which corresponds to the $2(n-2) \mathbb E(I_1 I_2)$ term. Then, we handle the pairs that are more than one apart from one another by using the independence remarked in the preceding comments: $$\mathbb E \left[ \dots + 2 \sum_{j < k,\ |j - k| > 1} I_j I_k \right] = \dots + 2\sum_{j < k,\ |j-k| > 1} \mathbb E( I_j) \mathbb E( I_k)$$ Thus, our only task is to count how many terms are in that sum. The set $\{(j, k): 1 \leq j < k \leq n-1\}$ has $\binom{n-1}{2}$ elements, and as previously referenced $n-2$ of those consist of the pairs for which $|j-k| =1$; hence, there are $\binom{n-1}{2} - (n-2)$ terms in the sum. When doubled, that expression is equal to $(n-1)^2 - (n-1) - 2(n-2)$.