#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #if __has_include() #include using namespace atcoder; #endif #define GET_MACRO(_1, _2, _3, NAME, ...) NAME #define _rep(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++) #define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using i64 = long long; template bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; } template bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; } templateistream& operator>>(istream&i,vector&v){rep(j,v.size())i>>v[j];return i;} templatestring join(vector&v){stringstream s;rep(i,v.size())s<<' '<ostream& operator<<(ostream&o,vector&v){if(v.size())o<string join(vector>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;} templateostream& operator<<(ostream&o,vector>&vv){if(vv.size())o< using pq = priority_queue, greater>; const i64 INF = 1e18; using mint = modint998244353; #include #include using namespace std; vector fact(int x) { vector res(x + 1); res[0] = 1; rep(i, x) { res[i + 1] = res[i] * (i + 1); } return res; } vector finv(int x) { vector res(x + 1); mint t = 1; for(int i = 1; i <= x; i++) { t *= i; } res[x] = 1 / t; for(int i = x; i > 0; i--) { res[i - 1] = res[i] * i; } return res; } auto r = fact(200005); auto l = finv(200005); mint comb(int x, int y) { if(x - y < 0 || x < 0 || y < 0) return 0; return r[x] * l[y] * l[x - y]; } template std::istream &std::operator>>(std::istream &is, atcoder::static_modint &a) { long long v; is >> v; a = v; return is; } template std::istream &std::operator>>(std::istream &is, atcoder::dynamic_modint &a) { long long v; is >> v; a = v; return is; } template std::ostream &std::operator<<(std::ostream &os, const atcoder::static_modint &a) { return os << a.val(); } template std::ostream &std::operator<<(std::ostream &os, const atcoder::dynamic_modint &a) { return os << a.val(); } mint get(i64 n) { // 1~nの順列の転倒数の総和 if(n < 0) return 0; if(n == 0) return 0; return r[n] / 2 * n * (n - 1) / 2; } int main() { int n, m; cin >> n >> m; vector> v; rep(i, m) { int p, k; cin >> p >> k; p--; k--; v.emplace_back(k, p); } fenwick_tree fw(n); sort(all(v)); // 未確定 > 未確定 mint ans = get(n - m); // cout << "未確定 > 未確定" << endl; // cout << "ans : " << ans << endl; // 確定 > 確定 rep(i, m) { auto [k, p] = v[i]; ans += r[n - m] * fw.sum(p, n); fw.add(p, 1); } // cout << "確定 > 確定" << endl; // cout << "ans : " << ans << endl; int cnt = 0; // 確定 > 未確定 for(int i = m - 1; i >= 0; i--) { auto [k, p] = v[i]; mint now_ans = 1; if(n - m - 1 >= 0) { // cout << mint(p - fw.sum(0, p)) << " , " << n - 1 - k - cnt << endl; now_ans *= mint(p - fw.sum(0, p)) * (n - 1 - k - cnt) * r[n - m - 1]; ans += now_ans; // cout << "now_ans : " << now_ans << endl; } cnt++; } // cout << "確定 > 未確定" << endl; // cout << ans << endl; // 未確定 > 確定 cnt = 0; for(int i = 0; i < m; i++) { auto [k, p] = v[i]; mint now_ans = 1; // cout << "k : " << k << ", p : " << p << endl; if(n - m - 1 >= 0) { // cout << mint(n - 1 - p - fw.sum(p + 1, n)) << " , " << k - cnt << endl; // cout << "k : " << k << " , " << " cnt : " << cnt << endl; now_ans *= mint(n - 1 - p - fw.sum(p + 1, n)) * (k - cnt) * r[n - m - 1]; ans += now_ans; // cout << "now_ans : " << now_ans << endl; } cnt++; } // cout << "末確定 > 確定" << endl; cout << ans << endl; return 0; }