Divisible subarray

Calculation of prefixsum array :
we did
sum%=n;
sum = (sum+n)%5; (to resolve the negative index)

How we can say that the new positive sum updates at the
right position in prefixsum array

It doesn’t matter because we are calculating prefix sum modulus with some k and put on that index. The index is not related to the modulus because whatever the modulus is its going to be at that index.