How is the summation being expanded? I am trying to understand summations by solving some example problems, but I could not understand how is the second to last line being expanded? I would really appreciate if you could explain me how is it being expanded.
\begin{align}
&\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\sum_{k=1}^{j}1 =\\
&\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}j =\\
&\sum_{i=1}^{n-1}\left(\sum_{j=1}^{n}j - \sum_{j=1}^{i}j\right) =\\
&\sum_{i=1}^{n-1}\left(\frac{n(n+1)}{2} - \frac{i(i+1)}{2}\right) =\\
&\frac{1}{2}\sum_{i=1}^{n-1}n^2+n-i^2-i =\\
&\frac{1}{2}\left((n-1)n^2 + (n-1)n - \left(\frac{n(n+1)(2n+1)}{6} - n^2\right) - 
    \left(\frac{n(n+1)}{2} - n\right)\right) =\\
&f(n) = \frac{n(n(n+1))}{2} - \frac{n(n+1)(2n+1)}{12} - \frac{n(n+1)}{4}
\end{align}
 A: I take it that what has to be explained is this (I've introduced parentheses on the left hand side for clarity):
\begin{multline*}
\sum_{i=1}^{n-1}\left(n^2 + n - i^2 - i\right) = \\
(n-1)n^2 + (n-1)n - \left(\frac{n(n+1)(2n+1)}6 - n^2\right) - \left(\frac{n(n+1)}2 - n\right).
\end{multline*}
This equation results from adding together the following four identities:
\begin{align*}
\sum_{i=1}^{n-1}n^2 & = (n - 1)n^2, \\
\sum_{i=1}^{n-1}n & = (n - 1)n, \\
\sum_{i=1}^{n-1}i^2 & = \sum_{i=1}^ni^2 - n^2 \\ & = \frac{n(n+1)(2n+1)}6 - n^2, \\
\sum_{i=1}^{n-1}i & = \sum_{i=1}^ni - n \\ & = \frac{n(n+1)}2 - n.
\end{align*}
Lines 4 and 6 follow, of course, from the familiar identities:
\begin{align*}
\sum_{i=1}^ni^2 & = \frac{n(n+1)(2n+1)}6, \\
\sum_{i=1}^ni & = \frac{n(n+1)}2.
\end{align*}
I don't know why it was done this way! It seems to me that it would have been simpler just to write:
\begin{align*}
\sum_{i=1}^{n-1}i^2 & = \frac{(n-1)n(2n-1)}6, \\
\sum_{i=1}^{n-1}i & = \frac{(n-1)n}2.
\end{align*}
(Also, in the comments, I've suggested two ways to arrive at the final answer with less calculation.)
A: Starting from line 4 of the displayed equation, we have
$$\sum_{i=1}^{n-1}\frac{n(n+1)}{2}=(n-1)\frac{n(n+1)}{2}$$ because there are $n-1$ equal terms. Also,
$$\sum_{i=1}^{n-1}\frac{i(i+1)}{2}=\sum_{i=1}^{n-1}\left(\frac{i(i+1)(i+2)}{6}-\frac{(i-1)i(i+1)}{6}\right)$$ is a telescoping sum, hence equal to
$$\frac{(n-1)n(n+1)-0}6.$$
Therefore, the whole sum is equal to
$$
\frac{(n - 1)n(n + 1)}2 - \frac{(n - 1)n(n + 1)}6 = \frac{(n - 1)n(n + 1)}3.
$$

The "weird" decomposition above comes in fact from a general property of the raising factorials: e.g.
$$i(i+1)(i+2)(i+3)(i+4)-(i-1)i(i+1)(i+2)(i+3)=5i(i+1)(i+2)(i+3).$$
