Divisible Subarrays : count the number of good subarrays question

Could you please explain me the step :
sum=sum%n
sum=(sum+n)%n
Here in second step , as prateek bhaiya says, the sum can be negative; hence we do that;but i could not get it.

@shellcodingblock lets take an example, let sum be -7 and number be 9
now 9 % -7 is undefined
so when we do -7 + 9, it becomes 2
and 9 % 2 is defined

if we do % n at every step, it is guaranteed that the sum will never be equal to or greater than n, therefore if we add n to another, it is guaranteed to become positive even if it was negative before.