#P11748. Maximizing Weighted Prefix Sum in Lexicographic Permutations

    ID: 13840 Type: Default 1000ms 256MiB

Maximizing Weighted Prefix Sum in Lexicographic Permutations

Maximizing Weighted Prefix Sum in Lexicographic Permutations

You are given all n! permutations of length n arranged in lexicographical order. Consider the first S permutations in this order. For a permutation p = [p1, p2, ..., pn], define its weighted prefix sum as

[ \sum_{i=1}^{n}\sum_{j=1}^{i} p_j = \sum_{j=1}^{n}(n - j + 1),p_j. ]

Your task is to choose one permutation among the first S lexicographically ordered permutations which maximizes this value, and output the maximum value modulo 998244353.

Note: It is guaranteed that 1 \le S \le n!.

inputFormat

The input consists of a single line containing two integers n and S, where n is the length of the permutation and S denotes that only the first S lexicographically smallest permutations are available.

outputFormat

Output a single integer, the maximum weighted prefix sum achievable among the first S permutations, taken modulo 998244353.

sample

3 1
10