#include #include using namespace std; using namespace atcoder; // type typedef long long ll; typedef long double ld; template using pq = priority_queue; template using pqg = priority_queue, greater>; template using v = vector; #define pl pair #define vl v #define vp v #define vm v // IN-OUT #define NYAN ios::sync_with_stdio(false);cin.tie(nullptr);cout< void CIN(T &t, U &...u) { cin >> t; CIN(u...); } void COUT() { cout << "\n"; } template void COUT(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; COUT(u...); } #define dump(x) cerr << #x << ":"<< x << "\n"; #define vdump(x) rep(repeat, sz(x)) cerr << repeat << ":" << x[repeat] << "\n"; // macro #define bp __builtin_popcountll #define ALL(x) x.begin(),x.end() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define reps(i, s, n) for (ll i = s; i < (ll)(n); i++) #define sz(x) (ll)x.size() ll xd[]={0, 1, 0, -1, 1, 1, -1, -1}; ll yd[]={1, 0, -1, 0, 1, -1, -1, 1}; // function templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> n >> m; vl used(n), a(n); rep(i, m){ ll x, k; cin >> x >> k; a[k - 1] = x; used[x - 1] = 1; } vl vec, vec2; rep(i, n) if(used[i] == 0) vec.emplace_back(i + 1); mint ans = 0, base = 1; reps(i, 1, sz(vec)) base *= i; ll big = 0, small = sz(vec); rep(i, n){ if(a[i] == 0){ big++; small--; }else{ auto it = lower_bound(ALL(vec), a[i]); ll x = vec.end() - it; ans += x * big * base + (sz(vec) - x) * small * base; vec2.emplace_back(a[i]); } } mint add = 0; segtree sg(n + 1); rep(i, sz(vec2)){ auto x = vec2[i]; add += sg.prod(x, n + 1); sg.set(x, 1); } reps(i, 1, sz(vec) + 1) add *= i; ans += add; if(sz(vec)){ vm dp(sz(vec)); mint now = 1; dp[0] = 0; reps(i, 1, sz(vec)){ now *= i; dp[i] = now * ((1 + i) * i / 2) + dp[i - 1] * (i + 1); } ans += dp[sz(vec) - 1]; } cout << ans.val() << "\n"; } int main() { NYAN solve(); return 0; }