First let me ensure that the problem I am having is from a Physics book. But the problem is totally related to mathematics. Hence I think that this question here may not be illegitimate.
Using the concept of inverse square law of magnetic monopoles, this book derives an equation for PE between two magnetic monopoles:
$$M=-\int X dx=-PP'\int \dfrac{1}{r^2} \dfrac{x'-x}{r}dx=-PP'\int \dfrac{x'-x}{r^3} dx=\dfrac{PP'}{r}+C$$
If we define reference point of magnetic monopole at infinity, $C=0$ and PE at a point becomes: $M=\dfrac{PP'}{r}$
Now by adding all the dipoles of two magnets, the book derives an equation for PE between two magnets which is the same as PE between two closed circuits.
$$M=-\int X dx= \left( -\oint^{s'}_0 \oint^{s}_0 \dfrac{\vec{ds}.\vec{ds'}}{r}+C \right) ii'$$
Since reference position of all magnetic monopoles are defined at infinity, reference position of magnet is also at infinity. Therefore $C=0$ and PE at a position becomes:
$$M=-\int X dx=-\oint^{s'}_0 \oint^{s}_0 \dfrac{\vec{ds}.\vec{ds'}}{r}ii'$$
In another page, this book also derives another equation for PE between two closed circuits.
$$X=-\dfrac{\partial (-\oint^{s'}_0 \oint^{s}_0 \rho \text{ } {\vec{ds}.\vec{ds'})} }{\partial x}ii' =-\dfrac{\partial (-\oint^{s'}_0 \oint^{s}_0 \rho \text{ } {\vec{ds}.\vec{ds'}) +a+C} }{\partial x}ii'$$
where '$a$' is a constant
'$C$' is an arbitrary constant depending on reference point.
Therefore:
$$M=-\int X dx= \left[ \left( -\oint^{s'}_0 \oint^{s}_0 \rho \text{ } {\vec{ds}.\vec{ds'}} \right) +a+C \right] ii'$$
Now while equating the two potential energies:
We note that if we consider the same reference point, the two arbitrary constants (in the two equations of potential energies) are equal. Therefore:
$$-\oint^{s'}_0 \oint^{s}_0 \dfrac{\vec{ds}.\vec{ds'}}{r}ii' =\left[ \left( -\oint^{s'}_0 \oint^{s}_0 \rho \text{ } {\vec{ds}.\vec{ds'}} \right) +a \right] ii'$$
The book gives $\rho=\frac{1}{r}$ after equating the two potential energies. This is only possible if constant $a=0$. How can we ascertain that '$a$' must be zero?
You can actually begin with \begin{align} M &=-\int Xdx=\dfrac{qq{^\prime}}{r}+C \hspace{4mm}\mbox{ and } \\M& =-\int Xdx=p \text{ } qq{^\prime}+D, \end{align} where $C$ and $D$ are constants. By setting the two potentials equal to each other, we see that $$ \dfrac{qq{^\prime}}{r}+C = p \text{ } qq{^\prime}+D. $$ Since the constant terms must equate to each other, we see that $C$ must equal $D$. It also follows that $p=\frac{1}{r}$.