code
```cpp
class Solution {
public:
int numSubarraysWithSum(vector& nums, int goal) {
int n = nums.size();
map<int, int> mp;
int sum = 0;
int ans = 0;
mp[0] = 1;
for (int i = 0; i < n; i ++) {
sum += nums[i];
ans += mp[sum - goal];
mp[sum] ++;
}
return ans;
}
};
```
</details>