#P11748. 「TPOI-1B」ASPAP

「TPOI-1B」ASPAP

Description

You have n!n! permutations of length nn sorted in lexicographical order.

Among the first SS permutations in this order, find a permutation pp that maximizes i=1nj=1ipj\displaystyle\sum_{i=1}^n\sum_{j=1}^{i}p_j. Output this maximum value modulo 998244353998244353.

Input Format

The first line contains an integer TT.

Each of the next TT lines contains two integers nn and SS.

Output Format

For each query, output one integer representing the maximum value modulo 998244353998244353.

1
4 5
23

Hint

Explanation for Sample #1

The first five permutations of length 44 are:

  • $1, 2, 3, 4 \to 1 + (1+2) + (1+2+3) + (1+2+3+4) = 20$
  • $1, 2, 4, 3 \to 1 + (1+2) + (1+2+4) + (1+2+4+3) = 21$
  • $1, 3, 2, 4 \to 1 + (1+3) + (1+3+2) + (1+3+2+4) = 21$
  • $1, 3, 4, 2 \to 1 + (1+3) + (1+3+4) + (1+3+4+2) = 23$
  • $1, 4, 2, 3 \to 1 + (1+4) + (1+4+2) + (1+4+2+3) = 23$

The maximum value is 2323.

Constraints

This problem uses bundled tests. You must pass all test cases in a subtask to receive points.

Subtask Points Special Constraints
1 10 n8n \le 8
2 T20T \le 20, n16n \le 16
3 25 T104T \le 10^4
4 5 S=n!S = n!
5 50 No special constraints

For 100%100\% data: 1T1051 \le T \le 10^5, 1n1091 \le n \le 10^9, 1Smin(n!, 1018)1 \le S \le \min(n!,\ 10^{18}).

Translated by DeepSeek R1