Sep14 CHEFLR

I have referred this editorial link for SEP14 CHEFLR problem.

I too used the same logic during test. I addition to this I have added the mod operation(mod=1000000007). At each level I have done this.

ans=ans%mod;
if(0==ans)
{
  ans=mod.
}

Will this if condition cause any problem ? I dint find this condition in other successful submission

At the end:

ans=ans%mod;
printf ("%d\n", ans);

But faced WA when submitting the solution.

The complete code is available here

I faced the same with python. [http://discuss.codechef.com/questions/51095/chefflr-why-is-my-python-code-wrong]. I am sure someone has a good explanation. Admins any response?

In simple words Your problem is that u only checking whether the ans is 0 or not …actually us should check whether its negative or not…once the ans is negative …u must add “mod” value until it becomes positive…i.e.

while(ans<0)
{
    ans+=mod;
}
ans%=mod;

Hope it helps …