-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
103 lines (97 loc) · 1.77 KB
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "bits/stdc++.h"
using namespace std;
int n;
bool vis[200010];
int a[200010];
long long fac[200010];
const int mod = 998244353;
long long modpow(long long b, long long e) {
if(e == 0) return 1;
if(e & 1) return (modpow(b, e - 1) * b) % mod;
long long m = modpow(b, e >> 1);
return (m * m) % mod;
}
long long inverse(long long x) {
return modpow(x, mod - 2);
}
long long nC2(long long x) {
return ((x * (x-1)) / 2) % mod;
}
struct fenwick {
int t[200010];
fenwick () {
memset(t, 0, sizeof t);
}
void update(int x, int val) {
for(int i = x; i <= n; i += i & (-i)) {
t[i] += val;
}
}
int query(int x) {
int sum = 0;
for(int i = x; i > 0; i -= i & (-i)) {
sum += t[i];
}
return sum;
}
int query(int x, int y) {
if(x > y) return 0;
return query(y) - query(x-1);
}
} T, G;
int main(int argc, char const *argv[])
{
scanf("%d", &n);
fac[0] = 1;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if(a[i] != -1) {
vis[a[i]] = true;
}
fac[i] = fac[i - 1] * i;
fac[i] %= mod;
}
int total = 0;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
G.update(i, 1);
++total;
}
}
long long ans = 0;
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(a[i] != -1) {
if(cnt) {
ans += G.query(a[i] + 1, n) * ((cnt * fac[total - 1]) % mod);
ans %= mod;
}
} else {
++cnt;
}
}
cnt = 0;
for(int i = n; i >= 1; i--) {
if(a[i] != -1) {
if(cnt) {
ans += G.query(1, a[i] - 1) * ((cnt * fac[total - 1]) % mod);
ans %= mod;
}
} else {
++cnt;
}
}
ans += nC2(total) * ((fac[total] * inverse(2)) % mod);
ans %= mod;
ans *= inverse(fac[total]);
ans %= mod;
for(int i = 1; i <= n; i++) {
if(a[i] != -1) {
ans += T.query(a[i] + 1, n);
ans %= mod;
T.update(a[i], 1);
}
}
printf("%lld\n", ans);
return 0;
}