1# Runtime: 209 ms (Top 98.8%) | Memory: 16.64 MB (Top 51.7%)234class Solution:5def subarraysDivByK(self, nums, k):6n = len(nums)7prefix_mod = 08result = 0910# There are k mod groups 0...k-1.11mod_groups = [0] * k12mod_groups[0] = 11314for num in nums:15# Take modulo twice to avoid negative remainders.16prefix_mod = (prefix_mod + num % k + k) % k17# Add the count of subarrays that have the same remainder as the current18# one to cancel out the remainders.19result += mod_groups[prefix_mod]20mod_groups[prefix_mod] += 12122return result