Pattern Language
Time limit5sMemory limit512 MB
Each of M letters takes a digit up to its own limit u_i; count assignments that make the whole string a palindrome, where positions paired by mirror symmetry must get equal digits.
- Level
Medium6 of 10
- Topics
- Union-find, Math, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
There are distinct letters . You are given a string of length consisting of the characters . You want to replace each letter in this string with a digit so that the result is a palindrome. (A palindrome reads the same forwards and backwards.) Equal letters must be replaced by equal digits. Every given letter appears at least once in .
Letter can be replaced by an integer between and inclusive, without leading zeros. Count the number of replacement choices that make the resulting string a palindrome, modulo . Two choices are considered different if they assign different digits to any letter, even if the resulting string is the same.
Input
The input is given in the following format.
Output
Print the number of replacement choices modulo on one line.
Constraints
- ∈
- Each letter appears at least once in .
- are all distinct.