Here is the problem link:

link text

As mentioned in the STROPR editorial, I’m calculating the coefficients by

(M+3)!/ (4! * (M-1)! ) = M*(M+1)*(M+2)*(M+3)/4!

So, here is my code link:

link text

Here’s what I’m doing:

I’m going backwards from x.

Every loop, I calculate product = product * (M + extra) / (extra)

and then the total sum is stored in sums as sums = sums + Ax * product

and as the loop goes, the extra is incremented by one.

And then the output is sums % 1000000007

Subtasks 1 and 2 ran perfectly. But, it gave TLE in the 3rd subtask.

I don’t know where I’m making any mistake. I think that I’m using the least loops required to calculate the sum, but clearly I’m doing something wrong. Would someone please guide me in the right direction? Thanks, in advance.