結果

問題 No.2616 中央番目の中央値
ユーザー 👑 bo9chanbo9chan
提出日時 2024-01-27 08:06:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,064 ms / 2,000 ms
コード長 30,795 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 185,568 KB
実行使用メモリ 135,196 KB
最終ジャッジ日時 2024-09-28 09:35:10
合計ジャッジ時間 20,310 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 7 ms
6,940 KB
testcase_14 AC 11 ms
6,940 KB
testcase_15 AC 21 ms
7,132 KB
testcase_16 AC 44 ms
11,136 KB
testcase_17 AC 71 ms
15,232 KB
testcase_18 AC 131 ms
23,040 KB
testcase_19 AC 300 ms
44,416 KB
testcase_20 AC 283 ms
44,340 KB
testcase_21 AC 637 ms
87,576 KB
testcase_22 AC 1,048 ms
133,404 KB
testcase_23 AC 1,035 ms
133,912 KB
testcase_24 AC 551 ms
132,892 KB
testcase_25 AC 556 ms
132,740 KB
testcase_26 AC 1,051 ms
133,532 KB
testcase_27 AC 1,013 ms
133,524 KB
testcase_28 AC 993 ms
134,044 KB
testcase_29 AC 1,012 ms
133,996 KB
testcase_30 AC 1,013 ms
135,196 KB
testcase_31 AC 1,064 ms
133,656 KB
testcase_32 AC 1,045 ms
135,192 KB
testcase_33 AC 1,013 ms
134,936 KB
testcase_34 AC 1,005 ms
134,000 KB
testcase_35 AC 1,031 ms
134,292 KB
testcase_36 AC 1,028 ms
135,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef __LOCAL
#include <mytemplate.hpp>  // プリコンパイル済みヘッダ ~/local/include/mytemplate.hpp.gch
#else
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream> 
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#endif
#pragma GCC target "no-avx"  // gcc12.2のバグ回避用。少し実行速度が落ちる
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using int128 = __int128_t;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
#define REP1(i, n) REP3(i, 0, n, 1)
#define REP2(i, s, n) REP3(i, s, n, 1)
#define REP3(i, s, n, d) for(ll i = (ll)(s); i < (ll)(n); i += (d))
#define REP_OVERLOAD(e1, e2, e3, e4, NAME,...) NAME
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define DEP1(i, n) DEP3(i, n, -1, 1)
#define DEP2(i, n, s) DEP3(i, n, s, 1)
#define DEP3(i, n, s, d) for(ll i = (ll)(n); (ll)(s) < i; i -= (d))
#define DEP_OVERLOAD(e1, e2, e3, e4, NAME,...) NAME
#define dep(...) DEP_OVERLOAD(__VA_ARGS__, DEP3, DEP2, DEP1)(__VA_ARGS__)
#define fore(e, a) for (auto&& e: (a))
#define len(a) (ll)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
/*********    C O N S T A N T    **********/
constexpr ll INF = LONG_LONG_MAX / 2 - 10000LL;  // 4,611,686,018,427,377,903  ~= 4.6e+18, 19桁
const double PI = acos(-1);
/*********       P A I R       **********/
template<class T, class U> inline pair<T, U>& operator+=(pair<T, U>& a, const pair<T, U> b);
template<class T, class U> inline pair<T, U>& operator-=(pair<T, U>& a, const pair<T, U> b);
template<class T, class U> inline pair<T, U>& operator*=(pair<T, U>& a, const pair<T, U> b);
template<class T, class U> inline pair<T, U>& operator/=(pair<T, U>& a, const pair<T, U> b);
template<class T, class U> inline pair<T, U>& operator%=(pair<T, U>& a, const pair<T, U> b);
template<class T, class U, class V> inline pair<T, U>& operator+=(pair<T, U>& a, const V b);
template<class T, class U, class V> inline pair<T, U>& operator-=(pair<T, U>& a, const V b);
template<class T, class U, class V> inline pair<T, U>& operator*=(pair<T, U>& a, const V b);
template<class T, class U, class V> inline pair<T, U>& operator/=(pair<T, U>& a, const V b);
template<class T, class U, class V> inline pair<T, U>& operator%=(pair<T, U>& a, const V b);
template<class T, class U, class V> inline pair<T, U> operator+(pair<T, U> a, const V& b);
template<class T, class U, class V> inline pair<T, U> operator-(pair<T, U> a, const V& b);
template<class T, class U, class V> inline pair<T, U> operator*(pair<T, U> a, const V& b);
template<class T, class U, class V> inline pair<T, U> operator/(pair<T, U> a, const V& b);
template<class T, class U, class V> inline pair<T, U> operator%(pair<T, U> a, const V& b);
template<class T, class U> inline pair<T, U> operator+(pair<T, U> a);
template<class T, class U> inline pair<T, U> operator-(pair<T, U> a);
template<class T, class U> istream& operator >>(istream& stream, pair<T, U>& a);
template<class T, class U> ostream& operator <<(ostream &stream, const pair<T, U>& a);
/*********       V E C T O R      **********/
template<class T> inline vector<T>& operator+=(vector<T>& a, const vector<T> b);
template<class T> inline vector<T>& operator-=(vector<T>& a, const vector<T> b);
template<class T> inline vector<T>& operator*=(vector<T>& a, const vector<T> b);
template<class T> inline vector<T>& operator/=(vector<T>& a, const vector<T> b);
template<class T> inline vector<T>& operator%=(vector<T>& a, const vector<T> b);
template<class T, class U> inline vector<T>& operator+=(vector<T>& a, const U b);
template<class T, class U> inline vector<T>& operator-=(vector<T>& a, const U b);
template<class T, class U> inline vector<T>& operator*=(vector<T>& a, const U b);
template<class T, class U> inline vector<T>& operator/=(vector<T>& a, const U b);
template<class T, class U> inline vector<T>& operator%=(vector<T>& a, const U b);
template<class T, class U> inline vector<T> operator+(vector<T> a, const U& b);
template<class T, class U> inline vector<T> operator-(vector<T> a, const U& b);
template<class T, class U> inline vector<T> operator*(vector<T> a, const U& b);
template<class T, class U> inline vector<T> operator/(vector<T> a, const U& b);
template<class T, class U> inline vector<T> operator%(vector<T> a, const U& b);
template<class T> inline vector<T> operator+(vector<T> a);
template<class T> inline vector<T> operator-(vector<T> a);
template<class T> istream& operator >>(istream& stream, vector<T>& a);
template<class T> ostream& operator <<(ostream& stream, const vector<T>& v);
template<class T> ostream& operator <<(ostream& stream, const vector<vector<T>>& vv);
template<class T> inline T Sum(const vector<T>& v) {return reduce(all(v));}  // v.size() == 0 のとき T() を返す
template<class T> inline T Max(const vector<T>& v) {assert(v.size()); return *max_element(all(v));}
template<class T> inline T Min(const vector<T>& v) {assert(v.size()); return *min_element(all(v));}
template<class T> inline ll Argmax(const vector<T>& v) {assert(v.size()); return max_element(all(v)) - v.begin();}
template<class T> inline ll Argmin(const vector<T>& v) {assert(v.size()); return min_element(all(v)) - v.begin();}
template<class T, class U> inline bool Contains(const vector<T>& v, const U& a) {return find(all(v), a) != v.end();}
template<class T> inline void Unique(vector<T>& v) {sort(all(v)); v.erase(unique(all(v)), v.end());}  // ソートされたユニーク値のvectorに書き換える, O(NlogN).
template<class T, class U> vector<T> make_vector(int n, U v) { return vector<T>(n, v); }
template <class T, class... Args> auto make_vector(int n, Args... args) {auto val = make_vector<T>(args...); return make_vector<decltype(val)>(n, move(val));}
/*********        S E T        **********/
template<class T> istream& operator >>(istream& stream, set<T>& st);
template<class T> ostream& operator <<(ostream& stream, const set<T>& st);
template<class T> inline T Max(const set<T>& st) {assert(st.size()); return *prev(st.end());}
template<class T> inline T Min(const set<T>& st) {assert(st.size()); return *st.begin();}
/*********   M U L T I S E T   **********/
template<class T> istream& operator >>(istream& stream, multiset<T>& st);
template<class T> ostream& operator <<(ostream& stream, const multiset<T>& st);
template<class T> inline T Max(const multiset<T>& st) {assert(st.size()); return *prev(st.end());}
template<class T> inline T Min(const multiset<T>& st) {assert(st.size()); return *st.begin();}
template<class T> inline bool EraseOne(multiset<T>& st, T x) {auto it=st.find(x); if (it!=st.end()) {st.erase(it); return true;} else return false;}  // 要素xを1つ削除. 消せたらtrueを返す.
/*********     S T R I N G     **********/
inline string ToUpper(const string& s) {string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::toupper); return t; }
inline string ToLower(const string& s) {string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::tolower); return t; }
inline char ToUpper(const char& c) {string s{c}, t; t.resize(s.size()); std::transform(all(s), t.begin(), ::toupper); return t[0]; }
inline char ToLower(const char& c) {string s{c}, t; t.resize(s.size()); std::transform(all(s), t.begin(), ::tolower); return t[0]; }
/*********       T R I O       **********/
template<class T1, class T2, class T3> struct trio {
    T1 first; T2 second; T3 third;
    // コンストラクタ・代入演算子
    trio() {first=T1(); second=T2(); third=T3();}
    trio(const T1& x) : first(x), second(x), third(x) {}
    trio(const T1& x, const T2& y, const T3& z) : first(x), second(y), third(z) {}
    trio(const trio& t) {first=t.first; second=t.second; third=t.third;}  // コピーコンストラクタ
    trio& operator =(const trio& t) {first=t.first; second=t.second; third=t.third; return *this;}
    // 比較演算子
    auto operator <=>(const trio&) const = default;
    // 単項演算子(+ -)
    trio operator +() const { return *this; }
    trio operator -() const { return (-1) * (*this); }
    // 複合代入演算子
    trio& operator +=(const trio& t) {first += t.first; second += t.second; third += t.third; return *this;}
    trio& operator -=(const trio& t) {first -= t.first; second -= t.second; third -= t.third; return *this;}
    trio& operator *=(const trio& t) {first *= t.first; second *= t.second; third *= t.third; return *this;}
    trio& operator /=(const trio& t) {first /= t.first; second /= t.second; third /= t.third; return *this;}
    trio& operator %=(const trio& t) {first %= t.first; second %= t.second; third %= t.third; return *this;}
    // 算術演算子
    friend trio operator +(const trio& lhs, const trio& rhs) {return trio(lhs) += rhs;}
    friend trio operator -(const trio& lhs, const trio& rhs) {return trio(lhs) -= rhs;}
    friend trio operator *(const trio& lhs, const trio& rhs) {return trio(lhs) *= rhs;}
    friend trio operator /(const trio& lhs, const trio& rhs) {return trio(lhs) /= rhs;}
    friend trio operator %(const trio& lhs, const trio& rhs) {return trio(lhs) %= rhs;}
    // 入出力
    template<class U1, class U2, class U3> friend istream& operator >>(istream&, trio<U1, U2, U3>&);
    template<class U1, class U2, class U3> friend ostream& operator <<(ostream&, const trio<U1, U2, U3>&);
    operator tuple<T1&, T2&, T3&>() {return tie(first, second, third);}
};
using tll = trio<ll, ll, ll>;
/*********     R A N D O M     **********/
struct Random {
    mt19937_64 rnd;
    Random() {
        random_device seed_gen;
        rnd.seed(seed_gen());
    }
    ll randint(ll a, ll b) {  // [a, b]
        uniform_int_distribution<ll> dist(a, b);
        return dist(rnd);
    }
    double randreal(double a, double b) {
        uniform_real_distribution<double> dist(a, b);
        return dist(rnd);
    }
    char randchar(char a, char b) {  // [a, b]
        uniform_int_distribution<ll> dist(a, b);
        return dist(rnd);
    }
};
/*********       P R I N T       **********/
template<class T> inline void print(const T& e);
template<class H, class... T> inline void print(const H& h, const T&... t);
template<class... T> inline void End(const T&... t);
/*********       D E B U G       **********/
#ifdef __LOCAL
#define debug(...) if(DEBUG) do{cout << '[' << #__VA_ARGS__ << "] ";debug_(__VA_ARGS__);}while(0)
#else
#define debug(...)
#endif
void dbg_(const long long& e);
template<class T> void dbg_(const T& e);
template<class T, class U> void dbg_(const pair<T, U>& p);
template<class T1, class T2, class T3> void dbg_(const trio<T1, T2, T3>& t);
template<class T> void debug_(const T& e);
template<class T> void debug_(const vector<T>& v);
template<class T> void debug_(const vector<vector<T>>& vv);
template<class T, class U> void debug_(const map<T, U>& mp);
template<class T, class U> void debug_(const vector<map<T, U>>& vm);
template<class T> void debug_(const set<T>& st);
template<class T> void debug_(const multiset<T>& st);
template<class T> void debug_(const vector<set<T>>& vs);
template<class T> void debug_(const vector<multiset<T>>& vs);
template<class H, class... T> void debug_(const H& h, const T&... t);
/*********       O T H E R S       **********/
template<class T, class U> inline bool chmin(T& a, U b) {if (a > b) {a = b; return true;} return false;}  // bは値渡し!
template<class T, class U> inline bool chmax(T& a, U b) {if (a < b) {a = b; return true;} return false;}
template<class T, class U> inline auto Mod(const T& a, const U& m) {return (a % m + m) % m;}  // 負もOK
template<class T, class U> inline auto Ceil(const T& x, const U& y) {return x < 0 ? x/y : (x + y - 1) / y;}  // 負もOK
template<class T, class U> inline auto Floor(const T& x, const U& y) {return -(Ceil(-x, y)); }  // 負もOK
inline ll Isqrt(ll n) {assert(n >= 0); ll x = round(sqrt(n)); while(x * x > n) --x; return x;}
inline ll Comb(ll n, ll r) {if (r < 0 || n < r) return 0; r = min(r, n - r); ll ret = 1; rep(i, r) {ret *= n - i; ret /= i + 1;} return ret;}  // n=60, r=30までOK
template<class T> inline T Pow(T x, ll n) {assert(n >= 0); T ret = 1; while(1) {if (n % 2) ret *= x; n /= 2; if(!n) return ret; x = x * x;}}  // べき乗 mintもOK
template<class T> inline T Aseries(T a, T d, ll n) {assert(n >= 0); return a * n + n * (n - 1) / 2 * d;}  // 等差級数 mintもOK
template<class T> inline T Gseries(T a, T r, ll n) {assert(n >= 0); if (r == 1) return a * n; else return a * (1 - Pow(r, n)) / (1 - r);}  // 等比級数 mintもOK
template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
inline bool Bit(ll b, int i) {assert(0 <= i && i < 64); return (b >> i) & 1;}
inline ll Popcount(ll b) {return __builtin_popcountll(b);}
inline ll Mask(ll n) {assert(0 <= n && n < 63); return (1LL << n) - 1LL;}  // [0, n)のbitが立った64bit整数を返す。
inline ll Mask(ll n, ll m) {assert(n >= m); return Mask(n) ^ Mask(m);}  // [m, n)のbitが立った64bit整数を返す。
inline void PrintYesNo(bool b) {b ? print("Yes") : print("No");}
/*********       M  O  D       **********/
#include <atcoder/modint>  // https://atcoder.github.io/ac-library/production/document_ja/
using namespace atcoder;
using mint = modint998244353;     // modint1000000007;
istream& operator >>(istream& stream, mint& e) {ll n; stream >> n; e = n; return stream; }
ostream& operator <<(ostream& stream, const mint& e) { stream << e.val(); return stream; }
using vm = vector<mint>;
using vvm = vector<vm>;
/***************************************/
constexpr bool DEBUG = true;


// nCr % MODおよびnPr % MODを計算。n, r ~ 10^6まで可能。要atcoder/modint
// コンストラクタ:O(N), get: O(1).
// 参考:https://drken1215.hatenablog.com/entry/2018/06/08/210000
struct CombMod {
    ll N = 1;
    ll MOD = mint::mod();
    vm fac = {1, 1};   // fac[i]  = i!
    vm inv = {0, 1};   // inv[i]  = 1/i (i > 0)
    vm finv = {1, 1};  // finv[i] = 1/i!
    CombMod() { }
    // 前計算を先に実施(高速化)
    CombMod(ll N) {
        assert(1 <= N);
        extend(N);
    }
    // N -> N2に増やし、fac, inv, finvのresize & 計算を実行
    void extend(ll N2) {
        if (N2 <= N) return;
        fac.resize(N2 + 1);
        inv.resize(N2 + 1);
        finv.resize(N2 + 1);
        rep(i, N + 1, N2 + 1) {
            fac[i] = fac[i - 1] * i;
            // modintのinv()は使わない。計算量がO(log MOD)のため。
            inv[i] = MOD - inv[MOD % i] * (MOD / i);
            finv[i] = finv[i - 1] * inv[i];
        }
        N = N2;
    }
    // nCr % MOD
    mint nCr(ll n, ll r) {
        if (r < 0 || n < r) return 0;
        if (N < n) extend(n);
        return fac[n] * finv[r] * finv[n - r];
    }
    // nPr % MOD
    mint nPr(ll n, ll r) {
        if (r < 0 || n < r) return 0;
        if (N < n) extend(n);
        return fac[n] * finv[n - r];
    }
    // nHr % MOD (= n+r-1Cr % MOD)
    mint nHr(ll n, ll r) {
        if (N < n + r - 1) extend(n + r - 1);
        return nCr(n + r - 1, n - 1);
    }
    // 多項係数 (Σa_i)! / Π(a_i !). vec = {}のとき1を返す.
    mint multi_coef(vl vec) {
        assert(vec.empty() || *min_element(all(vec)) >= 0LL);
        ll sum = reduce(all(vec));
        if (sum > N) extend(sum);
        mint ret = fac[sum];
        fore(e, vec) ret *= finv[e];
        return ret;
    }
};
// CombMod cm;
// mint ans1 = cm.nCr(100000, 50000);      // 149033233
// mint ans2 = cm.multi_coef(vl{2, 3, 4})  // 9!/(2!3!4!) = 1260


// 参考:https://www.youtube.com/watch?v=Id7lmNxg21w&t=1557s
struct WaveletTree {
    private:
        struct node {
            vector<int> rank0;  // rank0[r] : [0, r)における第hビットがゼロである要素数
            shared_ptr<node> left  = nullptr;  // 子ノード(左)
            shared_ptr<node> right = nullptr;  // 子ノード(右)
            vl sum0, sum1;      // [0, r)における第hビットが0/1である値の累積和
        };

        ll N;                   // 元配列のサイズ
        ll BIT_SIZE = 0;        // ビットサイズ(自動調整). 10^6: 19, 10^9: 29.
        shared_ptr<node> root;  // ルートノード

        // Wavelet Tree構築
        shared_ptr<node> build(vector<int>& a, ll h) {
            if (h == -1 || !a.size()) return nullptr;
            vector<vector<int>> b(2);
            fore(x, a) {
                ll idx = (x >> h) & 1;
                b[idx].pb(x);
            }

            shared_ptr<node> res(new node());
            res->left  = build(b[0], h - 1);
            res->right = build(b[1], h - 1);

            ll n = a.size();
            res->rank0.resize(n + 1);
            res->sum0.resize(n + 1);
            if (h == 0) res->sum1.resize(n + 1);

            rep(i, n) {
                bool iszero = (((a[i] >> h) & 1) == 0);
                res->rank0[i + 1] = res->rank0[i] + (ll)iszero;
                res->sum0[i + 1] = res->sum0[i] + a[i] * iszero;
                if (h == 0) res->sum1[i + 1] = res->sum1[i] + a[i] * !iszero;
            }
            return res;
        }

    public:
        WaveletTree(vector<int>& a) : N(a.size()) {
            fore(x, a) {
                assert(x >= 0);
                while(1) {
                    if ((1LL << (BIT_SIZE + 1)) > x) break;
                    BIT_SIZE++;
                }
            }
            root = build(a, BIT_SIZE);
        }

        // 区間[l, r) で v 以上の要素数
        ll count_ge(ll l, ll r, ll v) {
            assert(0 <= l && l <= r && r <= N);
            shared_ptr<node> x = root;
            ll h = BIT_SIZE;
            ll res = 0;

            while(1) {
                if (x == nullptr) {
                    res += r - l;
                    break;
                }
                ll l2 = x->rank0[l];
                ll r2 = x->rank0[r];
                if (((v >> h) & 1) == 0) {
                    res += (r - r2) - (l - l2);
                    l = l2;
                    r = r2;
                    x = x->left;
                } else {
                    l -= l2;
                    r -= r2;
                    x = x->right;
                }
                h--;
            }
            return res;
        }

        // 区間[l, r) で v 未満の要素数
        ll count_l(ll l, ll r, ll v) {
            return r - l - count_ge(l, r, v);
        }

        // 区間[l, r) で a 以上 b 未満の要素数
        ll count_range(ll l, ll r, ll a, ll b) {
            assert(a <= b);
            return r - l - count_ge(l, r, b) - count_l(l, r, a);
        }

        // 区間[l, r)のk番目最小値(k : 1-origin)
        ll kth_minimum(ll l, ll r, ll k) {
            assert(0 <= l && l <= r && r <= N && 0 < k && k <= r - l);
            shared_ptr<node> x = root;
            ll res = 0;

            while(1) {
                if (x == nullptr) break;
                res <<= 1;
                ll l2 = x->rank0[l];
                ll r2 = x->rank0[r];
                ll c = r2 - l2;
                if (c >= k) {  // [l, r)内でゼロがk個以上 -> bitは0確定
                    l = l2;
                    r = r2;
                    x = x->left;
                } else {       // [l, r)内でゼロがk個未満 -> bitは1確定
                    res++;
                    l -= l2;
                    r -= r2;
                    x = x->right;
                    k -= c;    // k > cなのでkは1未満には成り得ない。
                }
            }
            return res;
        }

        // 区間[l, r)のk番目最大値(k : 1-origin)
        ll kth_maximum(ll l, ll r, ll k) {
            return kth_minimum(l, r, r - l + 1 - k);
        }

        // 区間[l, r)のうち小さいほうからk個の和
        ll sum_k_smallest(ll l, ll r, ll k) {
            assert(0 <= l && l <= r && r <= N && 0 < k && k <= r - l);
            shared_ptr<node> x = root;
            ll h = BIT_SIZE;
            ll res = 0;

            while(h >= 0) {
                ll l2 = x->rank0[l];
                ll r2 = x->rank0[r];
                ll c = r2 - l2;
                if (h == 0) {
                    if (c >= k) {
                        res += (x->sum0[r] - x->sum0[l]) / c * k;
                    } else {
                        res += x->sum0[r] - x->sum0[l];
                        res += (x->sum1[r] - x->sum1[l]) / (r - l - c) * (k - c);
                    }
                } else {
                    if (c >= k) {  // [l, r)内でゼロがk個以上 -> bitは0確定
                        l = l2;
                        r = r2;
                        x = x->left;
                    } else {       // [l, r)内でゼロがk個未満 -> bitは1確定
                        res += x->sum0[r] - x->sum0[l];
                        l -= l2;
                        r -= r2;
                        x = x->right;
                        k -= c;    // k > cなのでkは1未満には成り得ない。
                    }
                }
                h--;
            }
            return res;
        }
};
// WaveletTree wt(vec);
// v 以上の要素数
// count_ge(ll l, ll r, ll v);
// v 未満の要素数
// count_l(ll l, ll r, ll v);
// a 以上 b 未満の要素数
// count_range(ll l, ll r, ll a, ll b);
// k番目最小値(k : 1-origin)
// kth_minimum(ll l, ll r, ll k);
// k番目最大値(k : 1-origin)
// kth_maximum(ll l, ll r, ll k);
// 小さいほうからk個の和
// sum_k_smallest(ll l, ll r, ll k);

// 解説AC
int main() {
    cin.tie(nullptr); cout << fixed << setprecision(10);

    ll N;
    cin >> N;
    vector<int> P(N);
    cin >> P;

    auto wt = WaveletTree(P);
    mint ans = 0;
    auto cm = CombMod();
    rep(i, N) {
        ll p = P[i];
        ll l1 = wt.count_l(0, i, p);
        ll l2 = wt.count_ge(0, i, p);
        ll r1 = wt.count_l(i + 1, N, p);
        ll r2 = wt.count_ge(i + 1, N, p);
        ans += cm.nCr(l1 + r2, l1) * cm.nCr(l2 + r1, l2);
    }
    print(ans);
}





/*********       P A I R       **********/
template<class T, class U> inline pair<T, U>& operator+=(pair<T, U>& a, const pair<T, U> b) {a.fi += b.fi; a.se += b.se; return a;}
template<class T, class U> inline pair<T, U>& operator-=(pair<T, U>& a, const pair<T, U> b) {a.fi -= b.fi; a.se -= b.se; return a;}
template<class T, class U> inline pair<T, U>& operator*=(pair<T, U>& a, const pair<T, U> b) {a.fi *= b.fi; a.se *= b.se; return a;}
template<class T, class U> inline pair<T, U>& operator/=(pair<T, U>& a, const pair<T, U> b) {a.fi /= b.fi; a.se /= b.se; return a;}
template<class T, class U> inline pair<T, U>& operator%=(pair<T, U>& a, const pair<T, U> b) {a.fi %= b.fi; a.se %= b.se; return a;}
template<class T, class U, class V> inline pair<T, U>& operator+=(pair<T, U>& a, const V b) {a.fi += b; a.se += b; return a;}
template<class T, class U, class V> inline pair<T, U>& operator-=(pair<T, U>& a, const V b) {a.fi -= b; a.se -= b; return a;}
template<class T, class U, class V> inline pair<T, U>& operator*=(pair<T, U>& a, const V b) {a.fi *= b; a.se *= b; return a;}
template<class T, class U, class V> inline pair<T, U>& operator/=(pair<T, U>& a, const V b) {a.fi /= b; a.se /= b; return a;}
template<class T, class U, class V> inline pair<T, U>& operator%=(pair<T, U>& a, const V b) {a.fi %= b; a.se %= b; return a;}
template<class T, class U, class V> inline pair<T, U> operator+(pair<T, U> a, const V& b) {a += b; return a;}
template<class T, class U, class V> inline pair<T, U> operator-(pair<T, U> a, const V& b) {a -= b; return a;}
template<class T, class U, class V> inline pair<T, U> operator*(pair<T, U> a, const V& b) {a *= b; return a;}
template<class T, class U, class V> inline pair<T, U> operator/(pair<T, U> a, const V& b) {a /= b; return a;}
template<class T, class U, class V> inline pair<T, U> operator%(pair<T, U> a, const V& b) {a %= b; return a;}
template<class T, class U> inline pair<T, U> operator+(pair<T, U> a) {return a;}
template<class T, class U> inline pair<T, U> operator-(pair<T, U> a) {return a * (-1);}
template<class T, class U> istream& operator >>(istream& stream, pair<T, U>& a) {stream >> a.fi >> a.se; return stream;}
template<class T, class U> ostream& operator <<(ostream &stream, const pair<T, U>& a) { stream << a.fi << " " << a.se; return stream; }
/*********       V E C T O R      **********/
template<class T> inline vector<T>& operator+=(vector<T>& a, const vector<T> b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] += b[i]; return a;}
template<class T> inline vector<T>& operator-=(vector<T>& a, const vector<T> b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] -= b[i]; return a;}
template<class T> inline vector<T>& operator*=(vector<T>& a, const vector<T> b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] *= b[i]; return a;}
template<class T> inline vector<T>& operator/=(vector<T>& a, const vector<T> b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] /= b[i]; return a;}
template<class T> inline vector<T>& operator%=(vector<T>& a, const vector<T> b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] %= b[i]; return a;}
template<class T, class U> inline vector<T>& operator+=(vector<T>& a, const U b) {fore(e, a) e += b; return a;}
template<class T, class U> inline vector<T>& operator-=(vector<T>& a, const U b) {fore(e, a) e -= b; return a;}
template<class T, class U> inline vector<T>& operator*=(vector<T>& a, const U b) {fore(e, a) e *= b; return a;}
template<class T, class U> inline vector<T>& operator/=(vector<T>& a, const U b) {fore(e, a) e /= b; return a;}
template<class T, class U> inline vector<T>& operator%=(vector<T>& a, const U b) {fore(e, a) e %= b; return a;}
template<class T, class U> inline vector<T> operator+(vector<T> a, const U& b) {a += b; return a;}
template<class T, class U> inline vector<T> operator-(vector<T> a, const U& b) {a -= b; return a;}
template<class T, class U> inline vector<T> operator*(vector<T> a, const U& b) {a *= b; return a;}
template<class T, class U> inline vector<T> operator/(vector<T> a, const U& b) {a /= b; return a;}
template<class T, class U> inline vector<T> operator%(vector<T> a, const U& b) {a %= b; return a;}
template<class T> inline vector<T> operator+(vector<T> a) {return a;}
template<class T> inline vector<T> operator-(vector<T> a) {return a * (-1);}
template<class T> istream& operator >>(istream& stream, vector<T>& a) {fore(e, a) stream >> e; return stream;}
template<class T> ostream& operator <<(ostream& stream, const vector<T>& v) {if(v.size()){stream << v[0]; rep(i, 1, v.size()) cout << " " << v[i];} return stream;}
template<class T> ostream& operator <<(ostream& stream, const vector<vector<T>>& vv) {if(vv.size()){stream << vv[0]; rep(i, 1, vv.size()) cout << '\n' << vv[i];} return stream;}
/*********        S E T        **********/
template<class T> istream& operator >>(istream& stream, set<T>& st) {T e; stream >> e; st.insert(e); return stream;}
template<class T> ostream& operator <<(ostream& stream, const set<T>& st) {if(st.size()){auto it=st.begin(); stream << *it++; for(; it!=st.end(); it++) cout << " " << *it;} return stream;}
/*********   M U L T I S E T   **********/
template<class T> istream& operator >>(istream& stream, multiset<T>& st) {T e; stream >> e; st.insert(e); return stream;}
template<class T> ostream& operator <<(ostream& stream, const multiset<T>& st) {if(st.size()){auto it=st.begin(); stream << *it++; for(; it!=st.end(); it++) cout << " " << *it;} return stream;}
/*********       P R I N T       **********/
template<class T> inline void print(const T& e) {cout << e << '\n';}
template<class H, class... T> inline void print(const H& h, const T&... t) {cout << h << ' '; print(t...);}
template<class... T> inline void End(const T&... t) {print(t...); exit(0);}
/*********       D E B U G       **********/
void dbg_(const long long& e) {if (e == INF) cout << "INF"; else if (e == -INF) cout << "-INF"; else cout << e;}
template<class T> void dbg_(const T& e) {cout << e;}
template<class T, class U> void dbg_(const pair<T, U>& p) {cout << '('; dbg_(p.first); cout << ' '; dbg_(p.second); cout << ')';}
template<class T1, class T2, class T3> void dbg_(const trio<T1, T2, T3>& t) {cout << '('; dbg_(t.first); cout << ' '; dbg_(t.second); cout << ' '; dbg_(t.third); cout << ')';}
template<class T> void debug_(const T& e) {dbg_(e); cout << '\n';}
template<class T> void debug_(const vector<T>& v) {if (v.size()){auto it=v.begin(); dbg_(*it++); for(; it!=v.end(); ++it){cout << ' '; dbg_(*it);}} cout << '\n';}
template<class T> void debug_(const vector<vector<T>>& vv) {cout << '\n'; ll cnt=0; for(auto&& v : vv){cout << cnt++ << ": "; debug_(v);}}
template<class T, class U> void debug_(const map<T, U>& mp) {if (mp.size()) {auto it = mp.begin(); dbg_(*it++); for(; it != mp.end(); ++it) {cout << ' '; dbg_(*it);}} cout << '\n';}
template<class T, class U> void debug_(const vector<map<T, U>>& vm){cout << '\n'; ll cnt=0; for(auto&& mp : vm){cout << cnt++ << ": "; debug_(mp);}}
template<class T> void debug_(const set<T>& st) {if(st.size()){auto it=st.begin(); dbg_(*it++); for(; it!=st.end(); ++it) {cout << ' '; dbg_(*it);}}cout << '\n';}
template<class T> void debug_(const multiset<T>& st) {if(st.size()) {auto it=st.begin(); dbg_(*it++); for(; it != st.end(); ++it) {cout << ' '; dbg_(*it);}} cout << '\n';}
template<class T> void debug_(const vector<set<T>>& vs) {cout << '\n'; ll cnt=0; for(auto&& st : vs){cout << cnt++ << ": "; debug_(st);}}
template<class T> void debug_(const vector<multiset<T>>& vs) {cout << '\n'; ll cnt=0;for(auto&& st : vs){cout << cnt++ << ": "; debug_(st);}}
template<class H, class... T> void debug_(const H& h, const T&... t) {dbg_(h); cout << ", "; debug_(t...);}
/*********       T R I O       **********/
template<class T1, class T2, class T3> istream& operator >>(istream &stream, trio<T1, T2, T3>& t) {return stream >> t.first >> t.second >> t.third;}
template<class T1, class T2, class T3> ostream& operator <<(ostream &stream, const trio<T1, T2, T3>& t) {return stream << t.first << " " << t.second << " " << t.third;}
0