---
{"category_name":"easy","problem_code":"SUMSUB","problem_name":"Subarray Sum","problemComponents":{"constraints":"- $1 \\leq T \\leq 5\\cdot 10^3$\n- $1 \\leq N \\leq 2\\cdot 10^5$\n- $1 \\leq A_i \\leq 10^9$\n- Sum of $N$ over all test cases does not exceed $10^6$.\n","constraintsState":true,"subtasks":"","subtasksState":false,"inputFormat":"- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.\n- The first line of each test case contains an integer $N$.\n- The second line contains $N$ space-separated integers $A_1, A_2, \\dots, A_N$.","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer - the required sum i.e $\\sum\\limits_{i=1}^n \\sum\\limits_{j=i}^nf(i, j)$ modulo $(10^9 + 7)$.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\n2\n1 2\n3\n1 2 3\n4\n5 4 1 3\n\n\n\n","output":"6\n20\n82\n","explanation":"**Test case $1$**: \n\n- $f(1, 1) = A_1 = 1$.\n- $f(1, 2) = A_1 + max(A_1, A_2) = 1 + max(1, 2) = 1 + 2 = 3$.\n- $f(2, 2) = A_2 = 2$. \n\nHence the required sum $= 1 + 3 + 2 = 6$.\n\n**Test case $2$**: \n\n- $f(1, 1) = A_1 = 1$.\n- $f(1, 2) = A_1 + max(A_1, A_2) = 1 + max(1, 2) = 1 + 2 = 3$.\n- $f(1, 3) = A_1 + max(A_1, A_2) + max(A_1, A_2, A_3) = 1 + max(1, 2) + max(1, 2, 3) = 1 + 2 + 3 = 6$.\n- $f(2, 2) = A_2 = 2$. \n- $f(2, 3) = A_2 + max(A_2, A_3) = 2 + max(2, 3) = 2 + 3 = 5$.\n- $f(3, 3) = A_3 = 3$. \n\nHence the required sum $= 1 + 3 + 6 + 2 + 5 + 3 = 20$.","isDeleted":false}}},"video_editorial_url":"","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"soumyadeep_21","problem_tester":"","date_added":"4-10-2021","tags":{"0":"dynamic","1":"easy","2":"soumyadeep_21","3":"stack","4":"start16"},"problem_difficulty_level":"Unavailable","best_tag":"Dynamic Programming","editorial_url":"https://discuss.codechef.com/problems/SUMSUB","time":{"view_start_date":1634751002,"submit_start_date":1634751002,"visible_start_date":1634751002,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=SUMSUB","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
Chef has a sequence of $N$ integers $A_1, A_2,\dots, A_N$. He defines a function $f(i, j)$ as follows:
$f(i, j) = A_i + max(A_i, A_{i + 1}) + max(A_i, A_{i + 1}, A_{i + 2}) + \dots + max(A_i, A_{i + 1}, \dots, A_j)$
Chef tries to find the sum of $f(i, j)$ over all $(i, j)$ such that $1 \leq i \leq j \leq N$ $(i.e \sum\limits_{i=1}^n \sum\limits_{j=i}^nf(i, j))$, but fails. Can you help him find the sum?
Since the sum can be very large, find it modulo $(10^9 + 7)$.
**Note:** Since the size of the input and output is large, please use fast input-output methods.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>