CF BUDDY
← Problems·

1906H · Twin Friends

2200 · combinatorics, dp

Problem: You meet two new friends who are twins. The name of the elder twin is AA, which consists of NN characters. While the name of the younger twin is BB, which consists of MM characters. It is known that NMN \leq M.

You want to call each of them with a nickname. For the elder twin, you want to pick any permutation of AA as the nickname. For the younger twin, you want to remove exactly MNM - N characters from any permutation of BB. Denote the nicknames of the elder twin and the younger twin as AA' and BB', respectively.

You want the nicknames to satisfy the following requirement. For each ii that satisfies 1iN1 \leq i \leq N, BiB'_i must be equal to either AiA'_i or the next letter that follows alphabetically after AiA'_i (if such a next letter exists).

Determine the number of different pairs of nicknames (A,B)(A', B') that satisfy the requirement. Two pairs of nicknames are considered different if at least one of the nicknames are different. As the result might be large, find the answer modulo 998244353998\,244\,353.

Input Format: The first line consists of two integers NN MM (1NM2000001 \leq N \leq M \leq 200\,000).

The second line consists of a string AA of length NN.

The third line consists of a string BB of length MM.

All strings consist of only upper-case letters.

Output Format: Output a single integer representing number of different pairs (A,B)(A', B') that satisfy the requirement, modulo 998244353998\,244\,353.

Note: Explanation for the sample input/output #1

The 99 pairs are:

  • (AAM, AAN),
  • (AAM, ABN),
  • (AAM, BAN),
  • (AMA, ANA),
  • (AMA, ANB),
  • (AMA, BNA),
  • (MAA, NAA),
  • (MAA, NAB), and
  • (MAA, NBA).

Explanation for the sample input/output #2

The 120120 pairs are the pairs where AA' is a permutation of BINUS and B=AB' = A'.

Sample Cases

Case 1

Input

3 4
AMA
ANAB

Output

9

Case 2

Input

5 8
BINUS
BINANUSA

Output

120

Case 3

Input

15 30
BINUSUNIVERSITY
BINANUSANTARAUNIVERSITYJAKARTA

Output

151362308

Case 4

Input

4 4
UDIN
ASEP

Output

0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback