Some times partial calculation exceeds the given modulo in that case it becomes difficult to determine where(the part of the partial calculation) iI need ti put modulo.

eg:

if modulo is put at the end of the final ans then it might happens the final ans exceeds the range of the data type and negative value comes out and we find modulo of the negative ans.

1 Like

Try using unsigned int or unsigned data types,its a good way to handle overflow while computation.This is if you donâ€™t require modulo operation.If you are using Modulo arithmetic,then just apply Modulo at every step.That wouldnâ€™t create any trouble while calculating answers.Do study about Modular arithmetic and all its properties.It will help you solve such problems with ease.Also do have a look at Eulerâ€™s Totient Theorem and Fermatâ€™s Little Theorem.

Here

**Happy Coding!!!**

1 Like