If prefix[j]−prefix[i]=k, then the subarray from i+1 to j has sum k. Rearranging: prefix[i]=prefix[j]−k.
For each j, count how many i<j have prefix[i]=prefix[j]−k. That's how many subarrays ending at j have sum k.
Use a hash map to count occurrences of each prefix sum. For each new prefix sum, look up how many times (current−k) has appeared.