Counting Palindromes
View as PDFBob works at the Cookie Factory, where cookies are baked in flavours numbered to
.
Every evening, a conveyor belt carries a sequence of
cookies past him.
Alice's birthday is coming, and so Bob wants to sneak her a gift, a palindromic cookie box.
A palindromic box of cookies is a subsequence (not necessarily continuous) of cookies chosen from the belt (in order) whose flavour sequence reads the same forwards and backwards.
Alice is picky though: she won't accept a box unless it contains at least cookies.
Bob doesn't want to get fired, so he'll pack the smallest valid palindromic box possible so he's less likely to get caught.
Formally, for a given conveyor belt arrangement , define
as the length of the
shortest palindromic subsequence of length at least
. If no such subsequence exists,
.
Since the factory runs many different arrangements over many nights, Bob wants to know the
sum of over all possible conveyor belt arrangements of
cookies where each cookie
can have a flavour from
to
. That is, there are
possible conveyor belt
arrangements.
Help Bob figure out the sum (modulo ) before Alice's birthday.
Input
A single line containing two integers and
.
Output
A single integer, the answer modulo .
Constraints
Example 1
Input
2 2
Output
4
Explanation
There are possible conveyor belt arrangements.
So the sum is .
Example 2
Input
3 3
Output
27
Explanation
There are possible conveyor belts,
of which have
and the rest have
.
Example 3
Input
2026 2
Output
937223623
Example 4
Input
2026 3
Output
979361356
Note
represents both the number of flavours and the number of cookies on the conveyor belt.
Comments