This page is still under construction.

Parts of this page are still being built. What you see may change.

The Palindromes Strike Back

Time limit2sMemory limit1024 MB

Summary
For every position i, count the subsets of positions that include i and form a palindrome, then XOR all i times that count mod 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Combinatorics, String, Math
Solved
No attempts yet

Problem

Palindromes have been a recurring theme in programming contests through the ages, but the problems built around them have usually been rather easy — and that makes the palindromes feel unappreciated. So, at the World Congress of Palindromes, it was decided that the palindromes must unite their power and put competitive programmers in their place.

Palindromes are cunning and often hide themselves inside strings. A palindrome is hidden in a string when, after deleting some set of characters, the characters that remain form a palindrome. For example, the string banaan hides the palindromes aaa, naan, nan, b, and so on.

Every character of the string has a palindromic power. This power equals the character's position number (counting from 11) multiplied by the number of palindromes hidden at that character. Two ways of deleting are considered different whenever the set of kept positions differs — even if the two resulting strings are identical. In other words, we count subsets of positions, not distinct strings.

For example, the palindromic powers of the four characters of aaba are 1⋅5=51 \cdot 5 = 5, 2⋅5=102 \cdot 5 = 10, 3⋅3=93 \cdot 3 = 9, and 4⋅6=244 \cdot 6 = 24. Why is the first character's power 55? Deleting different combinations of the other characters yields 88 subsets that keep the first character; of those, 55 are palindromes (marked with an asterisk), where a dot denotes a deleted character: a...*, a..a*, a.b., a.ba*, aa..*, aa.a*, aab., aaba.

When the palindromes "unite their power", they pool all of their bits together to become overwhelmingly strong. But they overlooked two things.

First, their power is bounded by a law of nature of programming contests known as the Magic Modulus, whose value is 109+710^9 + 7. When computing the palindromic power of a position, you must reduce that product modulo the Magic Modulus.

Second, the bits of the powers annihilate one another, so combining them yields not the sum of the powers but their bitwise XOR (⊕\oplus). The XOR of the palindromic powers of all characters of the string is called the palindromic power of the string.

Input

The first line contains the length of the string NN (1≤N≤30001 \le N \le 3000). The second line contains a string of NN lowercase Latin letters (a–z).

Output

Print, on a single line, the palindromic power of the given string.

Examples4

  1. Example 1

    Input
    4
    aaba
    
    Expected output
    30
    
  2. Example 2

    Input
    4
    abcd
    
    Expected output
    4
    
  3. Example 3

    Input
    5
    tcoct
    
    Expected output
    60
    
  4. Example 4

    Input
    62
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    
    Expected output
    1025495382