Why does $\sum_{j=i+1}^{n}(n-i-j+1) = n^{2}-5 n-2-i^{2}-3i$ Reading through https://stackoverflow.com/q/40696784/15753188 , the following calculation is performed


I see the rightmost summation is summing $(n-i-j+1)$, $n-(i+1)$ times. My first thought is to multiply $(n-(i+1))$ and $(n-i-(i+1)+1)$ (replacing the $j$ with $i+1$), but this gives me a wildly different answer than $n^{2}-5 n-2-i^{2}-3i$.

First, let's show that the equation doesn't hold.
If $n=1$, $$\sum_{i=1}^1 \sum_{j=i+1}^1(1-i-j+1) = 0$$
but $$\sum_{i=1}^1 (1^2-5-2-i^2-3i)=-10.$$

\begin{align}
\sum_{j=i+1}^n (n-i-j+1) &= \sum_{j=i+1}^n (n-i+1) - \sum_{j=i+1}^n j \tag{1}\\
&=(n-i+1)(n-i) - \frac{(n-i)(n+i+1)}{2}\\
&=(n-i) \cdot \frac{2n-2i+2 - n-i-1}{2}\\
&= (n-i)\cdot \frac{n-3i+1}2 \\
&=\frac12\cdot  \left(n^2 + (1-4i)n +3i^2-i\right)
\end{align}
The first term in $(1)$ is evaluated using multiplication but the second term is evaluated using an arithmetic sum. Multiplication by $k$ where $k$ is an integer means summing up the same number $k$ times.
