For the testcase:
1
4 49
858 723 332 138
your program is giving (Edit: actually, I’m getting a different result every time, so there’s some Undefined Behaviour/ uninitialised data in there):
140725646481792 4 2 94828364422956
when it should be:
976 723 332 138