# In python how to effectively use mod 10^9 in contest

I wanted to know if it is fine to print the final answer modulo 10^9+7 or should I use it in intermediate steps also.

Intermediate of course.

Try to understand the internal working of python first, How python takes input ?

Python reads the input stream and provide memory how much is required if available.

Which means your input wonâ€™t overflow untill it has buffer or memory but it is also possible to overflow in case buffer is full then it will flush the buffer thatâ€™s why intermediate mods are used to prevent overflow in every cases ; P

2 Likes

If you are using inbuilt â€śpowâ€ť function then itâ€™s better to send 3rd argument with it pow(x,y,mod). Itâ€™s faster than (x^y)%mod and looks better too. Although both give same results.

And also itâ€™s better to use it in intermediate steps too.

3 Likes

I code in Python from 1 yearâ€¦& what should I advice to you is that "using mod at intermediate steps is always fruitfulâ€¦definitely it will make your concepts about modulo crystal-clear as well as there will be no variance with the original answerâ€¦
you can pass 3rd argument as mod in pow()â€¦like pow(2,10,mod)â€¦it will return (2^10)%mod with fast exponentiation method

2 Likes

can you please give me and example where I should apply the mod in intermediate steps.

suppose you need to find out this :

(1000000000000 5 ) % 1000000007