I cant exactly say what causes the NZEC. But you should not try to use the factorial function. Look at the input constraints. fact(1000000) is really bigger than big. That takes a while to compute. It is recommended that you write the factorials on a piece of paper. You should observe that a lot of the multiplications are not needed. Once that is clear for you come up with a new approach.
Good luck.
Edit: I have quickly checked the submissions for MARBLES. Now I know the reason for the NZEC. You submitted your solution as 2.5. The factorial function is a new feature in all releases > 2.5. And I think your code is 3.X code. You can try to submit as 3.1.2. I bed 1 cent that you will get TLE now. So just follow the tips that I mentioned above.