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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <utility> #include <vector> #include <istream> #include <map> #include <cmath> #include <stack> #include <set> #include <cstring> #include <string> #include <fstream> #define ll long long #define maxn 200005 #define mdl 998244353 #define clr(a,n) for(int i=0;i<n;i++)a[i]=0 #define cfast std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pll pair<ll,ll> #define pii pair<int,int> #define inc(i,a,n) for(int i=a;i<n;i++) #define vset(a,n,m) for(ll i=0;i<n;i++)a[i]=m; #define endl '\n' using namespace std; ll fv[maxn], inv[maxn]; ll cpow(ll x, ll n) { ll ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mdl; x = (x * x) % mdl; n >>= 1; } return ans; } void getf(int n) { ll tmp = 1; inc(i, 1, n + 1) { fv[i] = tmp; tmp *= i + 1; tmp %= mdl; } } ll a[maxn * 4], temp[maxn], ad[4 * maxn],cnt[maxn]; void addq(ll o, ll l, ll r,ll x,int ql,int qr) { if (ql <= l && r <= qr) { a[o] += (r - l + 1) * x; ad[o] += x; return; } int m = (l + r) / 2; int ls = 2 * o, rs = 2 * o + 1; if (ql <= m) addq(ls, l, m,x,ql,qr); if (m < qr) addq(rs, m + 1, r, x,ql,qr); a[o] = a[ls] + a[rs] + (r - l + 1) * ad[o]; }
ll query(ll o, ll l, ll r, ll d,ll ql,ll qr) { if (qr == 0)return 0; if (ql <= l && r <= qr) return a[o] + d * (r - l + 1); ll m = (l + r) / 2; ll ls = 2 * o, rs = 2 * o + 1; ll ret = 0; if (ql <= m) ret += query(ls, l, m, d + ad[o],ql,qr); if (m < qr) ret += query(rs, m + 1, r, d + ad[o],ql,qr); return ret; } int main() { fv[0] = 1; int n, m; cin >> n >> m; ll cst = 200002; getf(cst); inc(i, 0, n) { ll tmp; cin >> tmp; cnt[tmp]++; addq(1, 1, cst,1,tmp + 1, cst); } ll res = 0; int f = 0; ll adder = 1; inc(i, 0, m) { ll tmp; cin >> tmp; if (i >= n) { f = 1; continue; } ll les = query(1, 1, cst, 0, tmp, tmp); res += adder*les %mdl* fv[n - i - 1] % mdl; res %= mdl; ll jud = query(1, 1, cst, 0, tmp + 1, tmp + 1) - les; if (jud == 0)break; adder *= jud; adder %= mdl; addq(1, 1, cst, -1, tmp + 1, cst);
} inc(i,1,cst+1){ if (cnt[i] > 1) { res *= cpow(fv[cnt[i]], mdl - 2); res %= mdl; } } ll ans = res + f; ans %= mdl; cout << ans; }
|