Divisible subaarays

If we are writing the second line, Is the first also important?

sum%=n;

sum=(sum+n)%n;

yes it is because if sum would it self had been a big number
then simple adding n to it would result to overflow and it is mainly used to handle the -ve value of sum if we encounter
so we are required to write both the lines to prevent integer overflow