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.
Till now I have only used final answer modulo 10^9+7 and have got the correct answer. If you guys can please help me with it

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