We need to read multiple test cases. For each test case, given heights of buildings (width 1 each), water trapped between them like the classic trapping rain water problem. However, careful: In the classic problem, the water trapped above each bar is min(left_max, right_max) - height[i] if positive. Sum over i. That yields total water volume, where each building is considered width 1. So sum = Σ max(0, min(max_left, max_right) - height[i]).
But note constraints: N up to 10^5, T up to 10. So O(N) per test case is fine.
We need to output answer modulo 10^9+7. Since the answer can be huge? Let's see: heights up to 10^7, N up to 1e5, so water could be up to around 10^7 * 1e5 = 10^12, which fits within 64-bit but we need mod 1e9+7, so we can compute sum as 64-bit and then mod. But careful: The sum might be up to about 10^12, which fits in 64-bit (max ~9e18). So we can compute using long long.
Edge Cases: Might need to be careful with negative values? but heights are positive. So answer is nonnegative.
Thus algorithm: For each test case:
- read N, then array A of N integers.
- compute left_max array: left_max[0]=A[0]; for i from 1 to N-1: left_max[i] = max(left_max[i-1], A[i]).
- compute right_max array: right_max[N-1]=A[N-1]; for i from N-2 downto 0: right_max[i] = max(right_max[i+1], A[i]).
- total = 0; for i from 0 to N-1: water = min(left_max[i], right_max[i]) - A[i]; if water>0: total += water.
- output total % MOD.
Alternatively, we can do two-pointer approach to reduce space O(1), but with N=1e5 it's fine.
But note: Mod is only applied to the final answer; water units are integer counts, not fractional.
Check sample: 3 2 1 4 5 -> left_max: [3,3,3,4,5]; right_max: [5,5,5,5,5]; min: [3,3,3,4,5] -> water: [0,1,2,0,0] -> total 3. Output matches.
Second: [1,2,3,4,5] -> left_max: [1,2,3,4,5]; right_max: [5,5,5,5,5]; min: [1,2,3,4,5]; water all 0 -> sum 0.
Hence correct.
But we must be cautious about modulo: Since water units are integers, we can sum in 64-bit and then mod.
Implementation details: Read input using fast input maybe; but with constraints it's fine with cin if sync disabled.
We need to handle T test cases.
One more nuance: The problem statement says "How much water would be collected between the buildings? Water would be collected between the buildings if a small sized building occurs between 2 big sized building i.e. if a building with small height occurs between buildings of relatively larger height." That is exactly the classic trapped water problem. There is no mention of water being able to leak off the ends; in the classic problem, water at the ends is not trapped because there's no bounding wall. So we ignore ends. That matches our method.
Thus answer is straightforward.
But wait: The sample explanation says: "for 2nd building it could collect 1 unit of water,3rd building could collect 2 units". That's exactly the water above each building based on min of max left and max right. So yes.
Thus code is simple.
But the problem might have hidden twist? Possibly they want water between buildings (i.e., in the gaps) not above the buildings? Actually the classic problem calculates water stored above each building, but the interpretation is water is trapped between buildings of width 1, so water that accumulates in the troughs. That is exactly the sum over each column of water height above that column. So it's fine.
Check edge: If N=1, no water.
All good.
Now note mod: 1000000007.
Thus we will write solve().
But the problem might be from some contest and they expect efficient code. So we should use faster I/O.
Implementation:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
if (N <= 2) {
cout << 0 << "\n";
continue;
}
vector<int> left_max(N);
left_max[0] = A[0];
for (int i = 1; i < N; ++i) left_max[i] = max(left_max[i-1], A[i]);
vector<int> right_max(N);
right_max[N-1] = A[N-1];
for (int i = N-2; i >= 0; --i) right_max[i] = max(right_max[i+1], A[i]);
long long total = 0;
for (int i = 0; i < N; ++i) {
int water = min(left_max[i], right_max[i]) - A[i];
if (water > 0) total += water;
}
cout << total % MOD << "\n";
}
return 0;
}
We might also use two-pointer technique to avoid arrays, but not necessary.
Let's think about integer overflow: water is computed as int; but heights up to 10^7, min up to 10^7, A[i] also up to 10^7, so water fits in int. Summing in long long is safe.
Time Complexity: O(T * N).
Memory: O(N) per test case.
All good.
But note: N up to 1e5, T up to 10, so memory total up to 1e6, fine.
Thus answer.
But the problem might be trick: They might consider water collected between buildings, meaning water that can stand in the gaps between buildings, not above them? In many "rain water trapping" problems, the buildings are considered as vertical bars, water accumulates in the empty spaces between them. That's exactly the same: water above each point is zero where there is a building? Wait careful: In the typical problem, we have an elevation map (like bars of width 1), and water can be trapped on top of the bars as well. But the bars themselves occupy the column; water sits on top of the bar if the bar is shorter than the water level. So the water collected above the bar is min(left_max,right_max)-height[i]. So it's correct.
But the wording "between the buildings" might imply water collects in the gaps between buildings (i.e., the spaces between them, not above them). However the buildings are placed side by side with no gap, so there is no space between buildings; they are adjacent. So water can only be on top of buildings. Actually physically, if you have rectangular buildings placed side by side, water would fill the voids above them, not between them. So it's the same.
Thus solution is as above.
Double-check with some other cases: e.g., heights: [5,1,5] -> left_max [5,5,5], right_max [5,5,5], min [5,5,5], water [0,4,0] -> total 4. That's correct: the middle building can trap 4 units.
Another: [2,1,2] -> water [0,1,0] =1. Good.
So answer is correct.
Thus final code as above.
But maybe we need to handle mod within sum? The total might exceed 2^63? Let's check: max water per building could be up to 10^7 (if min max is 10^7 and A[i]=0, but A[i]>=1, so water up to 10^7-1. For N up to 1e5, total max ~1e12, which is about 2^40, well within 2^63. So safe.
So answer is straightforward.
Now for final output: We'll provide solution in Python as well? The problem is in C++ but since it's a text response, we can give either. But usually for such solutions we can produce code in C++ or Python. The user didn't specify language. However as ChatGPT, we need to output the final answer, which is the code? The instruction: "We need to read multiple test cases. ... Thus answer." The conversation is about solving the problem. The final output should be code, maybe. But the user said "Andrew has recently moved... How much water would be collected... Input... Output... constraints... SAMPLE INPUT... Explanation". The user is providing a problem statement, likely expecting a solution. So I need to write a program that solves it. I'll provide the solution in C++.
Thus final answer: Provide the C++ code.
But also note: The problem might require to use modulus. In our code we mod at the end.
Edge: N <= 2 case: we can skip loops but not necessary.
Thus final code.
Also potential: Use two pointers for O(1) memory, but not needed.
So final answer:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while(t--) {
int n; cin >> n;
vector<int> h(n);
for(auto &x: h) cin >> x;
if(n <= 2) { cout << "0\n"; continue; }
vector<int> lmax(n), rmax(n);
lmax[0] = h[0];
for(int i=1; i<n; ++i) lmax[i] = max(lmax[i-1], h[i]);
rmax[n-1] = h[n-1];
for(int i=n-2; i>=0; --i) rmax[i] = max(rmax[i+1], h[i]);
long long total = 0;
for(int i=0; i<n; ++i) {
int water = min(lmax[i], rmax[i]) - h[i];
if(water > 0) total += water;
}
cout << total % MOD << "\n";
}
return 0;
}
Thus final. ```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> height(N);
for (int i = 0; i < N; ++i) cin >> height[i];
if (N <= 2) {
cout << "0\n";
continue;
}
vector<int> left_max(N), right_max(N);
left_max[0] = height[0];
for (int i = 1; i < N; ++i) left_max[i] = max(left_max[i-1], height[i]);
right_max[N-1] = height[N-1];
for (int i = N-2; i >= 0; --i) right_max[i] = max(right_max[i+1], height[i]);
long long total = 0;
for (int i = 0; i < N; ++i) {
int water = min(left_max[i], right_max[i]) - height[i];
if (water > 0) total += water;
}
cout << total % MOD << "\n";
}
return 0;
}