I'm a noob in mathematics and I'm now studying basic Maths for Machine Learning. Now I'm learning the equation of straight line y = mx + b. For the gradient descent process for the best fit line, I've learned that we need to use Partial Derivative for b (y-intercept) and m (slope).
In derivative equation for (m) slope, I am lost how the line1 is formulated to line2 in the following diagram.
I know the basic concepts of Calculus and Derivative. I'm lost how this Trinomial square is calculated from the first one to second one.



$$\begin{align}\frac d{dm}\left[\frac1{2n}\sum_{i=1}^n(mX_i+b-y_i)^2\right]&=\frac1{2n}\cdot\frac d{dm}\left[\sum_{i=1}^n(mX_i+b-y_i)^2\right]\\\text{(put derivative into summation)}&=\frac1{2n}\cdot\sum_{i=1}^n\left[\frac d{dm}(mX_i+b-y_i)^2\right]\\\text{(chain rule)}&=\frac1{2n}\cdot\sum_{i=1}^n\left[2(mX_i+b-y_i)\cdot\frac d{dm}(mX_i+b-y_i)\right]\\&=\frac1{n}\sum_{i=1}^n\left[(mX_i+b-y_i)\cdot X_i\right]\end{align}$$