Consider the two charges q1 and q2 initially at infinity and determine the work done by an external agency to bring the charges to the given locations.
Suppose, charge q1 is brought from infinity to the point r1. There is no external field against which work needs to be done, so work done in bringing q1 from infinity to r1 is zero. This charge produces a potential in space given by
The potential energy of a system of two charges
The potential energy of a system of 3 charges,