#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define fur(i, a, b) for(ll i = a; i <= (ll) b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll) b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define nl "\n"
const ll mod = 1e9L + 7;
const ll mxn = 3e5L + 5;
ll n;
pl p[mxn];
ll fact[mxn];
ll mul(const ll &a, const ll &b) {
ll c = (a % mod) * (b % mod);
c %= mod;
if(c < 0)
c += mod;
return c;
}
ll sum(const ll &a, const ll &b) {
ll c = (a % mod) + (b % mod);
c %= mod;
if(c < 0)
c += mod;
return c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fur(i, 1, n) {
cin >> p[i].fr;
p[i].sc = i;
}
sort(p + 1, p + n + 1);
ll a[n + 1];
fur(i, 1, n) {
a[p[i].sc] = i;
}
fact[0] = fact[1] = 1;
fur(i, 2, n) {
fact[i] = mul(fact[i - 1], i);
}
ll dp[n + 1] = {};
ll res =0;
fur(i, 1, n) {
ll smaller = a[i] - 1;
fur(j, 1, i - 1) {
if(a[j] < a[i]) {
--smaller;
}
}
dp[i] = mul(smaller, fact[n - i]);
res = sum(res, dp[i]);
}
res = sum(res, 1);
cout << res << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1084 ms |
11984 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1088 ms |
11924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
336 KB |
Output is correct |
14 |
Correct |
5 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
340 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
4 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
340 KB |
Output is correct |
34 |
Correct |
4 ms |
340 KB |
Output is correct |
35 |
Correct |
6 ms |
340 KB |
Output is correct |
36 |
Correct |
5 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
336 KB |
Output is correct |
38 |
Correct |
5 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
5 ms |
340 KB |
Output is correct |
41 |
Correct |
4 ms |
340 KB |
Output is correct |
42 |
Correct |
4 ms |
340 KB |
Output is correct |
43 |
Correct |
4 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
340 KB |
Output is correct |
45 |
Correct |
4 ms |
340 KB |
Output is correct |
46 |
Correct |
4 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
4 ms |
340 KB |
Output is correct |
49 |
Correct |
4 ms |
340 KB |
Output is correct |
50 |
Correct |
4 ms |
340 KB |
Output is correct |
51 |
Correct |
5 ms |
340 KB |
Output is correct |
52 |
Correct |
5 ms |
340 KB |
Output is correct |
53 |
Correct |
5 ms |
340 KB |
Output is correct |
54 |
Correct |
4 ms |
340 KB |
Output is correct |
55 |
Correct |
6 ms |
448 KB |
Output is correct |
56 |
Correct |
4 ms |
448 KB |
Output is correct |
57 |
Correct |
5 ms |
340 KB |
Output is correct |
58 |
Correct |
4 ms |
340 KB |
Output is correct |
59 |
Correct |
4 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
4 ms |
340 KB |
Output is correct |
63 |
Correct |
4 ms |
340 KB |
Output is correct |
64 |
Correct |
4 ms |
340 KB |
Output is correct |
65 |
Correct |
4 ms |
340 KB |
Output is correct |
66 |
Correct |
5 ms |
340 KB |
Output is correct |
67 |
Correct |
4 ms |
340 KB |
Output is correct |
68 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1088 ms |
11924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Execution timed out |
1084 ms |
11984 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |