Quote:Please give me the correct code

No!

This problem is typical from the challenges sites, this means that you failed to solve the problem.

These kind of problems are meant to be complicated, and not for beginners.

By the way, with these problems, simple minded solutions (brute force) are never the solution. You need to study the problem until you fully understand the inner working.

By the way, your code do not try to solve the problem. Read carefully the problem and try again.

You know, 1000000! goes like 10^5565706