description: Consider the insertion sort algorithm used to sort an integer sequence [a_1, a_2, …, a_n] of length n in non-decreasing order.
  
  For each i in order from 2 to n, do the following. If a_i ≥ a_{i-1}, do nothing and move on to the next value of i. Otherwise, find the smallest j such that a_i < a_j, shift the elements on positions from j to i-1 by one position to the right, and write down the initial value of a_i to position j. In this case we'll say that we performed an insertion of an element from position i to position j.
  
  It can be noticed that after processing any i, the prefix of the sequence [a_1, a_2, …, a_i] is sorted in non-decreasing order, therefore, the algorithm indeed sorts any sequence.
  
  For example, sorting [4, 5, 3, 1, 3] proceeds as follows: 
  
    * i = 2: a_2 ≥ a_1, do nothing; 
    * i = 3: j = 1, insert from position 3 to position 1: [3, 4, 5, 1, 3]; 
    * i = 4: j = 1, insert from position 4 to position 1: [1, 3, 4, 5, 3]; 
    * i = 5: j = 3, insert from position 5 to position 3: [1, 3, 3, 4, 5]. 
  
  
  
  You are given an integer n and a list of m integer pairs (x_i, y_i). We are interested in sequences such that if you sort them using the above algorithm, exactly m insertions will be performed: first from position x_1 to position y_1, then from position x_2 to position y_2, ..., finally, from position x_m to position y_m.
  
  How many sequences of length n consisting of (not necessarily distinct) integers between 1 and n, inclusive, satisfy the above condition? Print this number modulo 998 244 353.
  
  Input
  
  Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^5). Description of the test cases follows.
  
  The first line of each test case contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 0 ≤ m < n) — the length of the sequence and the number of insertions.
  
  The i-th of the following m lines contains two integers x_i and y_i (2 ≤ x_1 < x_2 < … < x_m ≤ n; 1 ≤ y_i < x_i). These lines describe the sequence of insertions in chronological order.
  
  It is guaranteed that the sum of m over all test cases does not exceed 2 ⋅ 10^5. Note that there is no constraint on the sum of n of the same kind.
  
  Output
  
  For each test case, print the number of sequences of length n consisting of integers from 1 to n such that sorting them with the described algorithm produces the given sequence of insertions, modulo 998 244 353.
  
  Example
  
  Input
  
  
  3
  3 0
  3 2
  2 1
  3 1
  5 3
  3 1
  4 1
  5 3
  
  
  Output
  
  
  10
  1
  21
  
  Note
  
  In the first test case, the algorithm performs no insertions — therefore, the initial sequence is already sorted in non-decreasing order. There are 10 such sequences: [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3].
  
  In the second test case, the only sequence satisfying the conditions is [3, 2, 1].
  
  In the third test case, [4, 5, 3, 1, 3] is one of the sought sequences.
solution: #include <bits/stdc++.h>
  using namespace std;
  const int inf = 1e9;
  const long long inf64 = 1e18;
  const int mod = 998244353;
  inline int _sum(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
  inline int _mul(int a, int b) { return (1ll * a * b) % mod; }
  inline int _sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
  inline void _inc(int& x, int y) {
    if ((x += y) >= mod) {
      x -= mod;
    }
  }
  inline void _dec(int& x, int y) {
    if ((x -= y) < 0) {
      x += mod;
    }
  }
  inline int _binpow(int x, int pw) {
    int res = 1;
    int tmp = x;
    while (pw > 0) {
      if (pw & 1) {
        res = _mul(res, tmp);
      }
      tmp = _mul(tmp, tmp);
      pw >>= 1;
    }
    return res;
  }
  inline int _div(int a, int b) { return _mul(a, _binpow(b, mod - 2)); }
  int slow(int n, int m, vector<pair<int, int> > a) {
    int res = 0;
    vector<int> p(n + 1);
    auto check = [&]() -> int {
      vector<int> pp = p;
      vector<pair<int, int> > b;
      for (int i = 2; i <= n; i++) {
        if (p[i] >= p[i - 1]) continue;
        int j = i - 1;
        while (j > 1 && p[j - 1] > p[i]) j--;
        int value = p[i];
        for (int q = i; q > j; q--) p[q] = p[q - 1];
        p[j] = value;
        b.emplace_back(i, j);
      }
      p = pp;
      return int(a == b);
    };
    function<void(int)> rec = [&](int i) {
      if (i > n) {
        _inc(res, check());
        return;
      }
      for (int x = 1; x <= n; x++) {
        p[i] = x;
        rec(i + 1);
      }
    };
    rec(1);
    return res;
  }
  mt19937 gen(42);
  struct Treap {
    struct Node {
      int prior = gen();
      Node* l = nullptr;
      Node* r = nullptr;
      int value = 0;
      int pu = 0;
      Node(int x) { value = x; }
    };
    void upd(Node* v, int x) {
      if (!v) return;
      v->value += x;
      v->pu += x;
    }
    void push(Node* v) {
      if (!v) return;
      upd(v->l, v->pu);
      upd(v->r, v->pu);
      v->pu = 0;
    }
    void merge(Node*& v, Node* l, Node* r) {
      if (!l || !r) {
        v = l ? l : r;
        return;
      }
      push(l);
      push(r);
      if (l->prior < r->prior) {
        merge(l->r, l->r, r);
        v = l;
      } else {
        merge(r->l, l, r->l);
        v = r;
      }
    }
    void split_by_value(Node* v, Node*& l, Node*& r, int x) {
      if (!v) {
        l = r = nullptr;
        return;
      }
      push(v);
      if (v->value >= x) {
        split_by_value(v->l, l, v->l, x);
        r = v;
      } else {
        split_by_value(v->r, v->r, r, x);
        l = v;
      }
    }
    Node* root = nullptr;
    Node *nl, *nm, *nr;
    Treap() = default;
    void insert(int x) {
      nl = nm = nr = nullptr;
      split_by_value(root, nl, nr, x);
      split_by_value(nr, nm, nr, x + 1);
      if (!nm) nm = new Node(x);
      merge(nl, nl, nm);
      merge(root, nl, nr);
    }
    void erase(int x) {
      nl = nm = nr = nullptr;
      split_by_value(root, nl, nr, x);
      split_by_value(nr, nm, nr, x + 1);
      merge(root, nl, nr);
    }
    void inc(int x) {
      nl = nr = nullptr;
      split_by_value(root, nl, nr, x);
      upd(nr, +1);
      merge(root, nl, nr);
    }
    int dfs(Node* v) {
      if (!v) return 0;
      return dfs(v->l) + dfs(v->r) + 1;
    }
    int size() { return dfs(root); }
  };
  const int N = 4e5 + 100;
  int fact[N];
  int rfact[N];
  void precalc() {
    fact[0] = 1;
    for (int i = 1; i < N; i++) fact[i] = _mul(fact[i - 1], i);
    rfact[N - 1] = _binpow(fact[N - 1], mod - 2);
    for (int i = N - 2; i >= 0; i--) rfact[i] = _mul(rfact[i + 1], i + 1);
  }
  int CNK(int n, int k) {
    if (n < k) return 0;
    return _mul(fact[n], _mul(rfact[k], rfact[n - k]));
  }
  int fast(int n, int m, vector<pair<int, int> > a) {
    Treap tr;
    for (auto [x, y] : a) {
      tr.inc(y);
      tr.insert(y);
      tr.erase(y - 1);
    }
    int sm = tr.size();
    int ub = n - 1 - sm;
    if (ub < 0) return 0;
    return CNK(ub + n, n);
  }
  void test() {
    mt19937 rnd(42);
    const int MAXN = 6;
    while (1) {
      int n = rnd() % MAXN + 2;
      int m = rnd() % n;
      vector<int> a(n - 1);
      for (int i = 0; i < n - 1; i++) a[i] = i + 2;
      shuffle(a.begin(), a.end(), rnd);
      a.resize(m);
      sort(a.begin(), a.end());
      vector<pair<int, int> > b(m);
      for (int i = 0; i < m; i++)
        b[i].first = a[i], b[i].second = rnd() % (a[i] - 1) + 1;
      int fs = fast(n, m, b);
      int sl = slow(n, m, b);
      if (fs == sl) {
        cout << "OK(" << fs << ")" << endl;
      } else {
        cout << "WA\n";
        cout << "exp = " << sl << "\n";
        cout << "fnd = " << fs << "\n";
        cout << n << " " << m << "\n";
        for (auto [x, y] : b) cout << x << " " << y << "\n";
        break;
      }
    }
  }
  void work() {
    int t;
    cin >> t;
    while (t--) {
      int n, m;
      cin >> n >> m;
      vector<pair<int, int> > a(m);
      for (auto& [x, y] : a) cin >> x >> y;
      int res = fast(n, m, a);
      cout << res << "\n";
    }
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    precalc();
    work();
    return 0;
  }
  