If , then the subarray from to has sum . Rearranging: .
For each , count how many have . That's how many subarrays ending at have sum .
Use a hash map to count occurrences of each prefix sum. For each new prefix sum, look up how many times has appeared.