**Analysis**

We have an array \(a_1,\dots,a_n\) of positive integers, and we want to cut it into contiguous segments so as to maximize

\[
  \sum_{\text{segments }[L..R]} \Bigl(\prod_{i=L}^R a_i\Bigr)\,.
\]

Call the maximum “profit‐sum” on the prefix of length \(i\) 
\[
  \text{dp}[i].
\]
A straightforward DP is
\[
  \text{dp}[i]
  = \max_{0\le j<i}\bigl(\text{dp}[j]+\prod_{k=j+1}^i a_k\bigr),
\]
but that is \(O(n^2)\) in the worst case and far too big for \(n=2\cdot10^5\).

A well‐known speed‐up works as follows:

1.  Observe that any run of 1’s only contributes +1 per element if you split them individually; if you try to merge a lone 1 onto one side, you lose profit whenever the neighboring segment’s product is \(\ge2\).  Thus the only really interesting indices to consider when we end a segment at \(i\) are the positions of those \(a_k>1\).

2.  We keep a vector `pos` of all indices of the recent elements \(>1\).  When we arrive at position \(i\):

   -  First we do the “default” transition
      \[
        \text{dp}[i]\;=\;\text{dp}[i-1]+(a_i==1?1:a_i),
      \]
     because even if \(a_i\) is big, forming the segment \([i..i]\) gives profit \(a_i\).

   -  Then we try to form one big segment that ends at \(i\), starting from each of the last few “>1” positions in `pos`.  Let
      \[
        \text{prodTrue} = \prod_{\text{that run}} a_k,
        \quad
        \text{prodMod}  = \bigl(\prod a_k\bigr)\bmod(10^9+7).
      \]
     We keep multiplying backwards, and each time we check
      \[
        \text{candidateTrue} 
          = \text{dpTrue}[j\!-\!1]\;+\;\text{prodTrue}, 
        \qquad
        \text{candidateMod}
          = \bigl(\text{dpMod}[j\!-\!1]+ \text{prodMod}\bigr)\bmod(10^9+7),
      \]
     updating \(\text{dp}[i]\) if we get a larger “true” profit, or if tied in the true value then a larger \(\bmod\) value.

3.  We do not need to scan *all* past “>1” positions back to the start.  In fact, once the running product 
   \(\text{prodTrue}\) exceeds some large cap (say \(10^{18}\)), we know that
   all even-earlier start positions would only make the product bigger by factors
   \(\ge2\) each time, so their true profit \(\text{dp}[j\!-\!1]+(\text{prodTrue}\times\cdots)\)
   is guaranteed to exceed anything we have so far.  Thus, at that first moment
   \(\text{prodTrue}\ge10^{18}\), instead of continuing, we just take the
   *entire* product of all \(>1\) that have appeared (we kept its \(\bmod\) in a
   prefix array) and compare that one last giant segment.

Because each \(a_i>1\) forces us to do at most \(\sim60\) backward multiplications
before the product exceeds \(10^{18}\), the total work is \(O(n\cdot60)\), easily
fast enough for \(n\le2\cdot10^5\).  We keep two DP‐arrays:

-  `dpTrue[i]` is capped at \(10^{18}\) (we only need it for comparisons),
-  `dpMod[i]` is the same optimum “sum of products” \(\bmod10^9+7\).

At the end we print `dpMod[n]`.

---

```cpp
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;

// We cap any "true" profit at INF = 1e18 to avoid 128-bit overflow.
static const __int128 INF = (__int128)1000000000000000000LL;  // 1e18

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    const int MOD = 1000000007;

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<int64> a(n+1);
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }

        // dpTrue[i] = the maximum profit (capped at 1e18) on prefix of length i
        // dpMod [i] = that same maximum profit mod 1e9+7
        vector<__int128> dpTrue(n+1, 0);
        vector<int64>   dpMod (n+1, 0);

        // We'll keep the recent indices where a[i] > 1
        vector<int> pos;
        // prefixProd[k] = product of a[pos[0]]..a[pos[k-1]] (mod MOD)
        // prefixProd[0] = 1
        vector<int64> prefixProd(1, 1);

        for(int i = 1; i <= n; i++){
            // 1) The "default" transition: take a[i] alone
            int64 add = (a[i] == 1 ? 1 : a[i]) % MOD;
            __int128 candTrue = dpTrue[i-1] + (__int128)(a[i]==1 ? 1 : a[i]);
            if(candTrue > INF) candTrue = INF;
            int64 candMod = (dpMod[i-1] + add) % MOD;

            dpTrue[i] = candTrue;
            dpMod [i] = candMod;

            // 2) If a[i]>1, we consider ending a big segment at i
            if(a[i] > 1){
                // record i in pos[]
                pos.push_back(i);
                prefixProd.push_back( (prefixProd.back() * (a[i] % MOD)) % MOD );

                int   m = (int)pos.size();
                __int128 prodTrue = 1;  // running product in true space
                int64     prodMod  = 1; // running product in mod space

                // scan backwards over at most ~60 >1's
                for(int t = m-1; t >= 0; t--){
                    int idx = pos[t];
                    int64 v = a[idx];

                    // multiply
                    prodTrue *= (__int128)v;
                    if(prodTrue >= INF){
                        // once we exceed INF, every even-earlier j'
                        // gives an even larger product, so the best
                        // "huge" segment is the one that starts
                        // at pos[0] and goes to i.
                        // Its mod-product is prefixProd[m].
                        __int128 bigTrue = INF;
                        int64    bigMod  = prefixProd[m];

                        if(bigTrue > dpTrue[i] ||
                          (bigTrue == dpTrue[i] && bigMod > dpMod[i])){
                            dpTrue[i] = bigTrue;
                            dpMod [i] = bigMod;
                        }
                        break;
                    }
                    prodMod = (prodMod * (v % MOD)) % MOD;

                    // candidate: segment from idx..i
                    __int128 nowTrue = dpTrue[idx-1] + prodTrue;
                    if(nowTrue > INF) nowTrue = INF;
                    int64    nowMod  = (dpMod[idx-1] + prodMod) % MOD;

                    if(nowTrue > dpTrue[i] ||
                      (nowTrue == dpTrue[i] && nowMod > dpMod[i])){
                        dpTrue[i] = nowTrue;
                        dpMod [i] = nowMod;
                    }
                }
            }
        }

        // Output the answer mod 1e9+7
        cout << dpMod[n] << "\n";
    }
    return 0;
}
```

**Explanation of Key Steps**

1.  **Default “split‐here” transition**  
    We always have the choice of taking \(a_i\) as a one‐element segment:
    \[
      \text{dp}[i] \;\leftarrow\; \text{dp}[i-1] \;+\; (a_i=1?1:a_i).
    \]
    We store that both in `dpTrue[i]` (capped) and in `dpMod[i]` (modulo).

2.  **“Merge back over recent >1’s”**  
    If \(a_i>1\), we keep a list `pos` of all indices \(j\) so far with \(a_j>1\).  
    We multiply backwards
    \[
      \prod_{\ell=t}^{m-1} a_{\,\text{pos}[\ell]}\,,
    \]
    both in 128-bit (`prodTrue`) and mod (`prodMod`).  Every time we land on an index `idx=pos[t]`, we form the segment
    \([\,\text{idx}\dots i]\) whose product is `prodTrue`; the total becomes
    \(\text{dpTrue}[{\rm idx}-1]+ \text{prodTrue}\).  We compare and keep the best.

3.  **Early break when product gets huge**  
    Since each new factor is at least 2, after at most about 60 steps the 128-bit
    product exceeds \(10^{18}\).  At that first point, any yet-earlier starting
    index would give an even *larger* product, so the single best such is the
    *earliest* one (`pos[0]`) merged all the way to \(i\).  We have already
    been maintaining in `prefixProd[m]` the product of all those \(>1\)’s
    mod \(10^9+7\), so we can in \(O(1)\) compare that final giant segment,
    cap its “true” value at \(10^{18}\), break, and move on.

Because each position \(i\) does at most one “default step” plus \(\le60\)
back-multiplications, the total is \(O(n\cdot60)\approx 1.2\times10^7\),
which runs in about a few tenths of a second for \(n=2\cdot10^5\).