As stated in the question, I am trying to write a generic solver for $2^{(x+1)^2}$ modulo $m$ where $m$ is not prime. Normally you can just do exponentiated squaring with modulus to get the answer, but the problem I have is that $(x+1)^2$ is too large to store in a standard datatype like unsigned long long in C++. Doing $2^{(x+1)^2~mod~m}~mod ~m$ is also wrong.
Is there any way to get around this problem?