#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for (int (i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++) #define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--) #define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using VD = vector; using VVD = vector; using PII = pair; using PDD = pair; using PLL = pair; using VPII = vector; template using VT = vector; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back template inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;} template inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;} const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; template, type LIM = (std::is_same>::value ? std::numeric_limits::max() : std::numeric_limits::min())> class SegTreeRMQ { private: int n; std::vector st; ctype comp; type query(int l, int r, int k, int L, int R) { if (R <= l || r <= L) return LIM; if (l <= L && R <= r) return st[k]; type res = LIM; int h = (L + R) >> 1; if (l < h) res = std::min(res, query(l, r, (k << 1) + 1, L, h), comp); if (h < r) res = std::min(res, query(l, r, (k << 1) + 2, h, R), comp); return res; } void update(int id, type el, int k, int L, int R) { if (id < L || R <= id) return; if (L + 1 == R && id == L) { st[k] = el; return; } int h = (L + R) >> 1; update(id, el, (k << 1) + 1, L, h); update(id, el, (k << 1) + 2, h, R); st[k] = std::min(st[(k << 1) + 2], st[(k << 1) + 1], comp); } public: SegTreeRMQ() : n(-1) {} SegTreeRMQ(int _n, type initval = LIM) : n(_n), st(_n << 2, initval){} type query(int l, int r) { assert(this->n != -1 && 0 <= l && r <= this->n && l <= r); return query(l, r, 0, 0, n); } void update(int id, type el) { assert(this->n != -1 && id < this->n); update(id, el, 0, 0, n); } }; const int MAX = 2e5 + 10; int n; SegTreeRMQ> seg; VI S() { VI ret; REP(k, n) { REP(i, n - k) { const int j = i + k; ret.emplace_back(seg.query(i, j + 1)); } } return ret; } int main() { scanf("%d", &n); seg = SegTreeRMQ>(n + 10); VI vv; REP(i, n) { int buf; scanf("%d", &buf); seg.update(i, buf); } VI v; REP(i, 2) { v = S(); if (i == 0) { seg = SegTreeRMQ>(v.size() + 10); REP(j, v.size()) { seg.update(j, v[j]); } n = v.size(); } } LL ans = 0; FORE(e, v) { (ans += e) %= MOD; } cout << ans << endl; }