이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
using namespace std;
int N;
int A[10005];
int DP[10005];
int Fact[10005], Inv[10005];
const int MOD = 1000000007;
int Power[10005];
int power(int n, int p)
{
    int sol = 1;
    while(p)
    {
        if(p % 2 == 1)
        {
            sol = (1LL * sol * n) % MOD;
        }
        p /= 2;
        n = (1LL * n * n) % MOD;
    }
    return sol;
}
void Read()
{
    cin >> N;
    for(int i = 1; i <= N; i++)
        cin >> A[i];
}
void precalcFact()
{
    Fact[0] = 1;
    for(int i = 1; i <= N; i++)
        Fact[i] = (1LL * Fact[i - 1] * i) % MOD;
    Inv[N] = power(Fact[N], MOD - 2);
    for(int i = N - 1; i >= 0; i--)
    {
        Inv[i] = (1LL * Inv[i + 1] * (i + 1)) % MOD;
    }
}
int Comb(int n, int k)
{
    return (1LL * Fact[n] * Inv[k] * Inv[n - k]) % MOD;
}
inline void Add(int& x, int y)
{
    x += y;
    if(x >= MOD)
        x -= MOD;
}
void precalcDP()
{
    DP[0] = 1;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 0; j < i; j++)
        {
            Add(DP[i], (1LL * DP[j] * Comb(i, j)) % MOD);
        }
    }
}
void Solve()
{
    int nb = 1, ans = 1;
    Power[0] = 1;
    for(int j = 1; j <= N; j++)
        Power[j] = j;
    for(int i = 2; i <= N; i++)
    {
        if(A[i] == 1)
            continue;
        int aux = 0;
        for(int j = 0; j <= N - i; j++)
        {
            Add(aux, (1LL * Comb(N - i, j) * DP[j] * Power[N - i - j]) % MOD);
        }
        Add(ans, (1LL * aux * (A[i] - 1)) % MOD);
        if(A[i] == nb + 1)
        {
            for(int j = 1; j <= N - i; j++)
            {
                Power[j] = (1LL * Power[j] * j) % MOD;
            }
            ++nb;
        }
    }
    cout << ans << "\n";
}
int main()
{
    Read();
    precalcFact();
    precalcDP();
    Solve();
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |