The problem lies with the upper limits on $m$ and $n$ in the definition of $H$. Going back to the integral,
$\int^{1}_{-1} \frac{d^2}{dx^2} x^{p + q - 2(m + n + 1)} dx$
which Mathematica returns as
(-1 - 2 (1 + m + n) + p + q) (-2 (1 + m + n) + p + q) * If[ Re[-2 m - 2 n + p + q] > 3, ... , Integrate[x^(-2 - 2 (1 + m + n) + p + q), {x, -1, 1}, Assumptions -> Re[-2 m - 2 n + p + q] <= 3] ]
which I've abbreviated for readability. The key is the condition $\Re(-2(m+n) + (p+q))>3$, which is not true for $m = (q - 1)/2$ and $n = (p-1)/2$. So, at that point, the false branch of If
is returned.
The difficulty is that you're trying to have Mathematica automatically do all the work for you, like in this similar question. When $m = (q - 1)/2$ and $n = (p-1)/2$, $p + q - 2(m + n + 1) = 0$ implying that the derivative should be zero. However, when the derivative was taken, Mathematica had no knowledge of this difficulty, so it gives the wrong result.
I'd do the following:
g = Piecewise[{ {Integrate[x^(p + q - 2*(m + n + 1)), {x, -1, 1}, Assumptions -> (-2 (m - n) + p + q) > 1], -2 (m - n) + p + q > 1}, {2, -2 m - 2 n + p + q == 1}}] h = Piecewise[{ {Integrate[D[x^(p + q - 2*(m + n + 1)),{x,2}], {x, -1, 1}, Assumptions -> (-2 (m - n) + p + q) > 3], -2 (m - n) + p + q > 3} }]
where h
does not require the second condition as it is zero anyway.