It is rather the third line that requires explanation. Fact is that and are independent, hence (for any ). And has same distribution as .

The aim of the second line is to reduce to independent random variables and thereby enable computation. Indeed and are correlated but, as second line shows, you can rewrite the formula in terms of and , which are independent.

And of course and are independent as well, so that this term is easily computed: .