We drag out of integral only what does not depend on the variable of integration, so from the point of view of integral it is a constant.

There is a more risky trick: changing the order of integration; it needs to be justified since the integrand are singular––but for the full rigour you need to go to MAT351 or even graduate course.