I have been trying to solve a problem related to a topic called random walk. I wanted to find the average position.
The equation I got is:
$$\bar x = \sum_{n=0}^N\frac{N!}{n!(N-n)!}(2n-N)ap^n q^{N-n}$$
Here $p$ is the probability of going right and $(1-p)$ is the probability of going left. I'm confused how the next equation has been done, specially the summed over part.
The way author did the next line is separating the equation, specially the red part.
$$\bar x = 2ap \frac{\partial}{\partial p}\sum_{n=0}^N\frac{N!}{n!(N-n)!}p^n q^{N-n} \ - \color{Red}{Na}$$
I'm adding how the author did his calculation which I did not understand the sum.

In the original equation, the author split the summation involving the $2n - N$ into $2$ separate summations, i.e.,
$$\sum_{n=0}^N\frac{N!}{n!(N-n)!}(2n)ap^n q^{N-n} + \sum_{n=0}^N\frac{N!}{n!(N-n)!}(-N)ap^n q^{N-n} \tag{1}\label{eq1}$$
The second part which was separated, i.e., $-Na\sum_{n=0}^N\frac{N!}{n!(N-n)!}p^n q^{N-n}$, gives a result of $-Na$. In particular, it means $\sum_{n=0}^N\frac{N!}{n!(N-n)!}p^n q^{N-n} = 1$. Note the LHS is the binomial expansion of $(p+q)^N$, so if $q = 1 - p \implies p + q = 1$, you'll get $\sum_{n=0}^N\frac{N!}{n!(N-n)!}p^n q^{N-n} = (p + q)^N = 1$, so the equation will be true. Note this is also a case of the sum of all possible probabilities among $N$ occurrences adding up to $1$.
As for the second equation's first term, the summed over part, note the partial derivative with respect to $p$ only affects the $p^n$ factor. Thus, you get
$$\begin{equation}\begin{aligned} 2ap \frac{\partial}{\partial p}\sum_{n=0}^N\frac{N!}{n!(N-n)!}p^n q^{N-n} & = 2ap \sum_{n=0}^N\frac{N!}{n!(N-n)!}(n)p^{n-1} q^{N-n} \\ & = \sum_{n=0}^N\frac{N!}{n!(N-n)!}(2n)ap^{n} q^{N-n} \end{aligned}\end{equation}\tag{2}\label{eq2}$$
As you can see, this is the first term in \eqref{eq1}, i.e., the original summation's expression without the $-N$ part in the brackets.