/** * author: tomo0608 * created: 01.05.2023 18:24:08 **/ #ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #pragma GCC optimize("Ofast") #include #include namespace tomo0608 { typedef long long ll; typedef long double ld; template using V = std::vector; template using VV = V>; template using VVV = V>; typedef std::pair pii; typedef std::pair pll; templatevoid input(T&... a) { (std::cin >> ... >> a); }; #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; input(__VA_ARGS__) #define STR(...) string __VA_ARGS__; input(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; input(__VA_ARGS__) std::istream& operator>>(std::istream& is, atcoder::modint998244353& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint998244353& a) { return os << a.val(); } std::istream& operator>>(std::istream& is, atcoder::modint1000000007& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint1000000007& a) { return os << a.val(); } template std::istream& operator>>(std::istream& is, atcoder::static_modint& a) { long long v; is >> v; a = v; return is; } template std::ostream& operator<<(std::ostream& os, const atcoder::static_modint& a) { return os << a.val(); } template std::istream& operator>>(std::istream& is, atcoder::dynamic_modint& a) { long long v; is >> v; a = v; return is; } template std::ostream& operator<<(std::ostream& os, const atcoder::dynamic_modint& a) { return os << a.val(); } template std::istream& operator>>(std::istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template std::ostream& operator<<(std::ostream& os, const std::pair& p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& e : v) is >> e; return is; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (auto& e : v) os << e << ' '; return os; } template std::ostream& operator << (std::ostream& os, std::set& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr;++itr;if (itr != set_var.end()) os << ", ";itr--; }os << "}";return os; } template std::ostream& operator<<(std::ostream& os, std::map& map_var) { os << "{";for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--; }os << "}";return os; } void print() { std::cout << '\n'; } templatevoid print(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; } void drop() { std::cout << '\n';exit(0); } templatevoid drop(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n';exit(0); } #ifdef __LOCAL void debug_out() { std::cerr << std::endl; } template < class Head, class... Tail> void debug_out(Head H, Tail... T) { std::cerr << ' ' << H; debug_out(T...); } #define debug(...) std::cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) std::cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << std::endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #define rep1(a) for(long long i = 0; i < a; i++) #define rep2(i, a) for(long long i = 0; i < a; i++) #define rep3(i, a, b) for(long long i = a; i < b; i++) #define rep4(i, a, b, c) for(long long i = a; i < b; i += c) #define drep1(a) for(long long i = a-1; i >= 0; i--) #define drep2(i, a) for(long long i = a-1; i >= 0; i--) #define drep3(i, a, b) for(long long i = a-1; i >= b; i--) #define drep4(i, a, b, c) for(long long i = a-1; i >= b; i -= c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define drep(...) overload4(__VA_ARGS__, drep4, drep3, drep2, drep1)(__VA_ARGS__) } // namespace tomo0608 int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }; int dy[8] = { 0, 1, 0, -1, 1, -1, -1, 1 }; #define endl '\n' namespace tomo0608 { #define all(x) x.begin(),x.end() template T SUM(const S& v) { return accumulate(all(v), T(0)); } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) template void zip(std::vector& x) { std::vector y = x;UNIQUE(y);for (int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template using priority_queue_rev = std::priority_queue, std::greater>; template inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return 1; } return 0; } template inline int count_between(std::vector& a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } template T mypow(T x, long long n) { T ret = 1;while (n > 0) { if (n & 1)(ret *= x);(x *= x);n >>= 1; }return ret; } } //typedef atcoder::modint1000000007 mint; typedef atcoder::modint998244353 mint; using namespace atcoder; using namespace std; using namespace tomo0608; #include namespace tomo0608 { // Binomial Coefficient of modint template struct BiCoef { std::vector fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].mod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr mint com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr mint perm(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[n - k]; } constexpr mint fact(int n) const noexcept { if (n < 0)return 0; return fact_[n]; } constexpr mint inv(int n) const noexcept { if (n < 0)return 0; return inv_[n]; } constexpr mint finv(int n) const noexcept { if (n < 0)return 0; return finv_[n]; } inline mint operator()(int n, int k) { return com(n, k); } constexpr mint com_naive(int n, int k) { if (n < k || n < 0 || k < 0)return 0; mint res = 1; k = std::min(k, n - k); for (int i = 1; i <= k; i++)res *= inv(i) * (n--); return res; } constexpr mint homo(int n, int r) { if (n < 0 || r < 0)return 0; return r == 0 ? 1 : com(n + r - 1, r); } }; } // namespace tomo0608 const ll inf = 1e18; void solve() { LL(n, m); BiCoef B(2 * n + m); V pos(n, -1), pos_inv(n, -1); // pos[i] = 数字iの位置 きまってないなら-1 V p_list(m), pos_list(m); rep(i, m) { LL(p, k); pos[--p] = --k; pos_inv[k] = p; p_list[i] = p; pos_list[i] = k; } sort(all(p_list)); sort(all(pos_list)); mint ans = B.com(n - m, 2) * B.fact(n - m) / 2; V v; v.reserve(m); rep(i, n) { if (pos_inv[i] == -1)continue; v.emplace_back(pos_inv[i]); } zip(v); fenwick_tree BIT(m); rep(i, m) { ans += BIT.sum(v[i] + 1, m) * B.fact(n - m); BIT.add(v[i], 1); } V left(n), right(n), small(n), big(n); // 数字xの位置がきまってるなら, ひだりのあきみぎのあきちいさいふめいおおきいふめい rep(i, m) { ll x = p_list[i]; small[x] = x - i; big[x] = (n - m) - small[x]; } rep(i, m) { ll now = pos_list[i]; ll x = pos_inv[now]; left[x] = now - i; right[x] = (n - m) - left[x]; } debug(small); debug(big); debug(left); debug(right); rep(i, m) { ll x = p_list[i]; ans += (big[x] * left[x] + small[x] * right[x]) * B.fact(n - m - 1); } print(ans); } int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::setprecision(20); int codeforces = 1; //cin >> codeforces; while (codeforces--) { solve(); } return 0; }