// URL : https://yukicoder.me/problems/no/1300 #pragma region optimize // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma endregion #include using namespace std; // #include #pragma region boost multiprecision // #include // #include // using Bint = boost::multiprecision::cpp_int; // using Bfloat32 = boost::multiprecision::number>; // using Bfloat1024 = boost::multiprecision::number>; #pragma endregion // #define int long long // #define endl '\n' #pragma region TEMPLATE // clang-format off /* TYPE */ typedef long long ll; typedef long double ld; typedef pair pii; typedef pair pll; typedef vector vpii; typedef vector vpll; typedef vector vi; typedef vector vl; typedef vector vst; typedef vector vb; typedef vector vld; typedef vector> vvi; template> using prique = priority_queue, Cmp>; template using prique_r = prique>; /* CONSTANT */ #define ln '\n' const int INF = 1 << 30; const ll INFF = 1LL << 60; const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int MOD = 1e9 + 7; const int MODD = 998244353; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; const double EPS = 1e-9; const ld PI = 3.14159265358979323846264338327950288; const int dx[] = { 1, 0, -1, 0, 1, -1, -1, 1, 0 }; const int dy[] = { 0, 1, 0, -1, -1, -1, 1, 1, 0 }; /* CONTAINER */ #define PB emplace_back #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(RALL(v)) #define LESS(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin()) #define LEQ(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin()) #define GREATER(x, val) (int)(x).size() - LEQ((x), (val)) #define GEQ(x, val) (int)(x).size() - LESS((x), (val)) #define UNIQUE(v) sort(ALL(v)); (v).erase(unique(ALL(v)), (v).end()) template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } template enable_if_t::value != 0> fill_v(U &u, const V... v) { u = U(v...); } template enable_if_t::value == 0> fill_v(U &u, const V... v) { for (auto &e : u) fill_v(e, v...); } /* LOOP */ #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REPI(i, 0, n) #define REPI(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i) #define REP(...) _overload3(__VA_ARGS__, REPI, _REP,)(__VA_ARGS__) #define _RREP(i, n) RREPI(i, n, 0) #define RREPI(i, a, b) for (ll i = (ll)a; i >= (ll)b; --i) #define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP,)(__VA_ARGS__) #define EACH(e, v) for (auto& e : v) #define PERM(v) sort(ALL(v)); for (bool c##p = true; c##p; c##p = next_permutation(ALL(v))) /* INPUT */ template void SSS(T& t) { cin >> t; } template void SSS(Head&& head, Tail&&... tail) { cin >> head; SSS(tail...); } #define SS(T, ...) T __VA_ARGS__; SSS(__VA_ARGS__); #define SV(T, v, n) vector v(n); for (auto& i : v) cin >> i; #define SVV(T, v, n, m) vector> v(n, vector(m)); for (auto& r : v) for (auto& i : r) cin >> i; /* OUTPUT */ // Yes / No inline int YES(bool x) { cout << (x ? "YES" : "NO") << endl; return 0; } inline int Yes(bool x) { cout << (x ? "Yes" : "No") << endl; return 0; } inline int yes(bool x) { cout << (x ? "yes" : "no") << endl; return 0; } inline int yES(bool x) { cout << (x ? "yES" : "nO") << endl; return 0; } inline int Yay(bool x) { cout << (x ? "Yay!" : ":(") << endl; return 0; } // PROTOTYPE DECLARATION template ostream &operator<<(ostream &os, const pair &j); template ostream &operator<<(ostream &os, const tuple &t); template::value, decltype(declval().begin(), nullptr)> = nullptr> ostream& operator<<(ostream &os, const C &c); template ostream &operator<<(ostream &os, const stack &j); template ostream &operator<<(ostream &os, const queue &j); template ostream &operator<<(ostream &os, const priority_queue &j); // IMPLEMENTATION template ostream &operator<<(ostream &os, const pair &j) { return os << '{' << j.first << ", " << j.second << '}'; } template enable_if_t PRINT_TUPLE(ostream &os, const tuple &t) {} template enable_if_t PRINT_TUPLE(ostream &os, const tuple &t) { os << get(t); if (num + 1 < sizeof...(T)) os << ", "; PRINT_TUPLE(os, t); } template ostream &operator<<(ostream &os, const tuple &t) { PRINT_TUPLE(os << '{', t); return os << '}'; } template::value, decltype(declval().begin(), nullptr)>> ostream& operator<<(ostream &os, const C &c) { os << '{'; for (auto it = begin(c); it != end(c); it++) { if (begin(c) != it) os << ", "; os << *it; } return os << '}'; } template ostream &operator<<(ostream &os, const stack &j) { deque d; for (auto c = j; !c.empty(); c.pop()) d.push_front(c.top()); return os << d; } template ostream &operator<<(ostream &os, const queue &j) { deque d; for (auto c = j; !c.empty(); c.pop()) d.push_back(c.front()); return os << d; } template ostream &operator<<(ostream &os, const priority_queue &j) { deque d; for (auto c = j; !c.empty(); c.pop()) d.push_front(c.top()); return os << d; } // OUTPUT FUNCTION template int PV(T &v) { int sz = v.size(); for (int i = 0; i < sz; ++i) cout << v[i] << " \n"[i == sz - 1]; return 0; } inline int print() { cout << endl; return 0; } template int print(Head&& head){ cout << head; return print(); } template int print(Head&& head, Tail&&... tail) { cout << head << " "; return print(forward(tail)...); } #ifdef LOCAL inline void dump() { cerr << endl; } template void dump(Head&& head) { cerr << head; dump(); } template void dump(Head&& head, Tail&&... tail) { cerr << head << ", "; dump(forward(tail)...); } #define debug(...) do {cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false) #else #define dump(...) #define debug(...) #endif /* OTHER */ #define fi first #define se second #define MP make_pair #define MT make_tuple template inline bool between(T x, A a, B b) { return ((a <= x) && (x < b)); } template inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return true; } return false; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline ll POW(ll a, ll b) { ll r = 1; do { if (b & 1) r *= a; a *= a; } while (b >>= 1); return r; } struct abracadabra { abracadabra() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(5); }; } ABRACADABRA; // clang-format on #pragma endregion #pragma region datastructure wavelet matrix struct SuccinctIndexableDictionary { size_t length; size_t blocks; vector bit, sum; SuccinctIndexableDictionary() = default; SuccinctIndexableDictionary(size_t length) : length(length), blocks((length + 31) >> 5) { bit.assign(blocks, 0U); sum.assign(blocks, 0U); } void set(int k) { bit[k >> 5] |= 1U << (k & 31); } void build() { sum[0] = 0U; for (int i = 1; i < blocks; i++) { sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]); } } bool operator[](int k) { return (bool((bit[k >> 5] >> (k & 31)) & 1)); } int rank(int k) { return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1))); } int rank(bool val, int k) { return (val ? rank(k) : k - rank(k)); } }; /* * @brief Wavelet-Matrix(ウェーブレット行列) * @docs docs/wavelet-matrix.md */ template struct WaveletMatrix { size_t length; SuccinctIndexableDictionary matrix[MAXLOG]; int mid[MAXLOG]; WaveletMatrix() = default; WaveletMatrix(vector v) : length(v.size()) { vector l(length), r(length); for (int level = MAXLOG - 1; level >= 0; level--) { matrix[level] = SuccinctIndexableDictionary(length + 1); int left = 0, right = 0; for (int i = 0; i < length; i++) { if (((v[i] >> level) & 1)) { matrix[level].set(i); r[right++] = v[i]; } else { l[left++] = v[i]; } } mid[level] = left; matrix[level].build(); v.swap(l); for (int i = 0; i < right; i++) { v[left + i] = r[i]; } } } pair succ(bool f, int l, int r, int level) { return { matrix[level].rank(f, l) + mid[level] * f, matrix[level].rank(f, r) + mid[level] * f }; } // v[k] T access(int k) { T ret = 0; for (int level = MAXLOG - 1; level >= 0; level--) { bool f = matrix[level][k]; if (f) ret |= T(1) << level; k = matrix[level].rank(f, k) + mid[level] * f; } return ret; } T operator[](const int &k) { return access(k); } // count i s.t. (0 <= i < r) && v[i] == x int rank(const T &x, int r) { int l = 0; for (int level = MAXLOG - 1; level >= 0; level--) { tie(l, r) = succ((x >> level) & 1, l, r, level); } return r - l; } // k-th(0-indexed) smallest number in v[l,r) T kth_smallest(int l, int r, int k) { assert(0 <= k && k < r - l); T ret = 0; for (int level = MAXLOG - 1; level >= 0; level--) { int cnt = matrix[level].rank(false, r) - matrix[level].rank(false, l); bool f = cnt <= k; if (f) { ret |= T(1) << level; k -= cnt; } tie(l, r) = succ(f, l, r, level); } return ret; } // k-th(0-indexed) largest number in v[l,r) T kth_largest(int l, int r, int k) { return kth_smallest(l, r, r - l - k - 1); } // count i s.t. (l <= i < r) && (v[i] < upper) int range_freq(int l, int r, T upper) { int ret = 0; for (int level = MAXLOG - 1; level >= 0; level--) { bool f = ((upper >> level) & 1); if (f) ret += matrix[level].rank(false, r) - matrix[level].rank(false, l); tie(l, r) = succ(f, l, r, level); } return ret; } // count i s.t. (l <= i < r) && (lower <= v[i] < upper) int range_freq(int l, int r, T lower, T upper) { return range_freq(l, r, upper) - range_freq(l, r, lower); } // max v[i] s.t. (l <= i < r) && (v[i] < upper) T prev_value(int l, int r, T upper) { int cnt = range_freq(l, r, upper); return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1); } // min v[i] s.t. (l <= i < r) && (lower <= v[i]) T next_value(int l, int r, T lower) { int cnt = range_freq(l, r, lower); return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt); } }; template struct CompressedWaveletMatrix { WaveletMatrix mat; vector ys; CompressedWaveletMatrix(const vector &v) : ys(v) { sort(begin(ys), end(ys)); ys.erase(unique(begin(ys), end(ys)), end(ys)); vector t(v.size()); for (int i = 0; i < v.size(); i++) t[i] = get(v[i]); mat = WaveletMatrix(t); } inline int get(const T &x) { return lower_bound(begin(ys), end(ys), x) - begin(ys); } T access(int k) { return ys[mat.access(k)]; } T operator[](const int &k) { return access(k); } int rank(const T &x, int r) { auto pos = get(x); if (pos == ys.size() || ys[pos] != x) return 0; return mat.rank(pos, r); } T kth_smallest(int l, int r, int k) { return ys[mat.kth_smallest(l, r, k)]; } T kth_largest(int l, int r, int k) { return ys[mat.kth_largest(l, r, k)]; } int range_freq(int l, int r, T upper) { return mat.range_freq(l, r, get(upper)); } int range_freq(int l, int r, T lower, T upper) { return mat.range_freq(l, r, get(lower), get(upper)); } T prev_value(int l, int r, T upper) { auto ret = mat.prev_value(l, r, get(upper)); return ret == -1 ? T(-1) : ys[ret]; } T next_value(int l, int r, T lower) { auto ret = mat.next_value(l, r, get(lower)); return ret == -1 ? T(-1) : ys[ret]; } }; #pragma endregion #pragma region math modint /** * @brief ModInt * @docs docs/math/modint.md */ template struct ModInt { using i32 = int; using i64 = long long; using u32 = unsigned int; using u64 = unsigned long long; u64 x; ModInt() : x(0) {} ModInt(i64 y) : x(set(y % MODULO + MODULO)) {} static u64 set(const i64 &y) { return (y < MODULO) ? y : y - MODULO; } ModInt operator+(const ModInt &m) const { return ModInt(set(x + m.x)); } ModInt operator-(const ModInt &m) const { return ModInt(set(x + MODULO - m.x)); } ModInt operator*(const ModInt &m) const { return ModInt(x * m.x % MODULO); } ModInt operator/(const ModInt &m) const { return ModInt(x) * ~ModInt(m.x); } ModInt &operator+=(const ModInt &m) { return *this = *this + m; } ModInt &operator-=(const ModInt &m) { return *this = *this - m; } ModInt &operator*=(const ModInt &m) { return *this = *this * m; } ModInt &operator/=(const ModInt &m) { return *this = *this / m; } ModInt &operator^=(const u64 &y) { return *this = *this ^ y; } ModInt operator~() const { return *this ^ (MODULO - 2); } ModInt operator-() const { return ModInt(set(MODULO - x)); } ModInt operator++() { return *this = ModInt(set(x + 1)); } ModInt operator--() { return *this = ModInt(set(x + MODULO - 1)); } bool operator<(const ModInt &m) const { return x < m.x; } bool operator>(const ModInt &m) const { return x > m.x; } bool operator==(const ModInt &m) const { return x == m.x; } bool operator!=(const ModInt &m) const { return x != m.x; } bool operator<=(const ModInt &m) const { return x <= m.x; } bool operator>=(const ModInt &m) const { return x >= m.x; } explicit operator u64() const { return x; } ModInt operator^(i64 y) const { return pow(x, y); } static ModInt pow(i64 x, i64 y) { bool neg = false; if (y < 0) y = -y, neg = true; ModInt u(1), t(x); while (y) { if (y & 1) u *= t; t *= t; y >>= 1; } return neg ? ModInt(1) / u : u; } friend ostream &operator<<(ostream &os, const ModInt &m) { return os << m.x; } friend istream &operator>>(istream &is, ModInt &m) { u64 y; is >> y; m = ModInt(y); return is; } }; constexpr int MODULO = (int)1e9 + 7; using modint = ModInt; #pragma endregion #pragma region datastructure segment tree /** * @brief セグメント木 * @docs docs/datastructure/segmenttree/segmenttree.md */ template struct SegmentTree { int N; const int sz; const T ID; const F f; vector cbt; SegmentTree(int n, T ID, F func) : sz(n), ID(ID), f(func) { init(sz); } SegmentTree(vector V, T ID, F func) : sz(V.size()), ID(ID), f(func) { init(sz); for (int i = 0; i < sz; ++i) cbt[i + N] = V[i]; for (int i = N - 1; i >= 1; --i) cbt[i] = f(cbt[i << 1 | 0], cbt[i << 1 | 1]); } void init(int n) { N = 1; while (N < n) N <<= 1; cbt.assign(N << 1, ID); } void update(int idx, T val) { assert(0 <= idx and idx < sz); idx += N; cbt[idx] = val; while (idx >>= 1) cbt[idx] = f(cbt[idx << 1 | 0], cbt[idx << 1 | 1]); } T query(int l, int r) { assert(0 <= l and l <= r and r <= sz); T ret_l = ID, ret_r = ID; l += N, r += N; while (l < r) { if (l & 1) ret_l = f(ret_l, cbt[l++]); if (r & 1) ret_r = f(cbt[--r], ret_r); l >>= 1, r >>= 1; } return f(ret_l, ret_r); } T query_all() { return cbt[1]; } template int rightmost(CF check, int l = 0) { assert(0 <= l and l <= sz); assert(check(ID)); if (l == sz) return sz; l += N; T acc = ID; do { while (l % 2 == 0) l >>= 1; if (not check(f(acc, cbt[l]))) { while (l < N) { l = 2 * l; if (check(f(acc, cbt[l]))) acc = f(acc, cbt[l++]); } return l - N; } acc = f(acc, cbt[l++]); } while ((l & -l) != l); return sz; } template int leftmost(CF check, int r = INT_MIN) { if (r == INT_MIN) r = sz; assert(0 <= r and r <= sz); assert(check(ID)); if (r == 0) return 0; r += N; T acc = ID; do { --r; while (r > 1 and (r % 2)) r >>= 1; if (not check(f(cbt[r], acc))) { while (r < N) { r = 2 * r + 1; if (check(f(cbt[r], acc))) acc = f(cbt[r--], acc); } return r + 1 - N; } acc = f(cbt[r], acc); } while ((r & -r) != r); return 0; } T const &operator[](int idx) const { return cbt[idx + N]; } void print() { for (int i = 1; i < 2 * N; ++i) { cerr << cbt[i] << ' '; if (!(i & (i + 1))) cerr << endl; } } }; template auto make_segment_tree(int N, T ID, F func) { return SegmentTree(N, ID, func); } template auto make_segment_tree(vector V, T ID, F func) { return SegmentTree(V, ID, func); } #pragma endregion #pragma region util compress /** * @brief 座標圧縮 * @docs docs/util/compress.md */ template pair, map> compress(vector ord) { vector com(ord); map rev; sort(com.begin(), com.end()); com.erase(unique(com.begin(), com.end()), com.end()); for (int i = 0; i < com.size(); ++i) rev[com[i]] = i; vector ret; for (auto &e : ord) ret.emplace_back(rev[e]); return make_pair(ret, rev); } #pragma endregion int solve(); signed main() { // int _T; cin >> _T; for (int t = 1; t <= _T; ++t) solve(); } using mint = ModInt; int solve() { SS(int, N); SV(int, A, N); auto [com, rev] = compress(A); int M = com.size(); int mx = *max_element(A.begin(), A.end()) + 1; int mn = 0; WaveletMatrix wav(A); vector L(N, 0), R(N, 0); REP (i, N) { // 自分の左に自分より大きい数が何個あるか if (i > 0) { L[i] = wav.range_freq(0, i, A[i] + 1, mx); } // 自分の右に自分より小さい数が何個あるか if (i < N - 1) { R[i] = wav.range_freq(i + 1, N, mn, A[i]); } } auto seg_add = [&](auto &seg, int idx, int val) -> void { seg.update(idx, seg[idx] + val); }; // 個数を管理 auto seg_l = make_segment_tree(N, 0, [](int a, int b) { return a + b; }); auto seg_r = make_segment_tree(N, 0, [](int a, int b) { return a + b; }); REP (i, N) { seg_add(seg_r, rev[A[i]], R[i]); } debug(L); debug(R); mint res = 0; REP (i, N) { int idx = rev[A[i]]; seg_add(seg_r, idx, -R[i]); mint cnt = (ll)L[i] * R[i]; cnt += seg_l.query(idx + 1, M); cnt += seg_r.query(0, idx); seg_add(seg_l, idx, L[i]); res += cnt * A[i]; } print(res); return 0; }