See this. The comment in line 40 demonstrates the reason. The pow function may also cause a range error if the result is too great or too small to be represented by a value of the return type. pow in this doesn’t return the correct value but gives inf if I try finding out for 2^10000 or more. Because of this, wherever in your code power exceeds the value, it starts giving erroneous results which gives you a wrong answer. Similarly, even your manual code is not working for larger values, but can be programmed to work if some mod condition etc is given.