Game Of Thrones - II
Difficulty: Hard

This follows from [Game of Thrones - I](https://www.hackerrank.com/contests/july13/challenges/game-of-thrones).

Now that the king knows how to find out whether a given word has an [anagram](https://en.wikipedia.org/wiki/Anagram) which is a [palindrome](http://en.wikipedia.org/wiki/Palindrome) or not, he encounters another challenge. He realizes that there can be more than one palindrome anagrams for a given word. Can you help him find out how many palindrome anagrams are possible for a given word ?

The king has many words. For each given word, he needs to find out the number of palindrome anagrams of the string. As the number of anagrams can be large, the king needs the number of anagrams % (10<sup>9</sup>+ 7).

**Input format :**<br>
A single line which contains the input string<br>

**Output format :**  
A single line which contains the number of anagram strings which are palindrome % (10<sup>9</sup> + 7).

**Constraints :**  
1<=length of string <= 10^5  
Each character of the string is a lowercase alphabet.  
Each test case has at least 1 anagram which is a palindrome. 
  
**Sample Input 01 :**

    aaabbbb

**Sample Output 01 :**

    3 

**Explanation :**  
Three palindrome permutations of the given string are abbabba , bbaaabb and bababab.

**Sample Input 02 :**

    cdcdcdcdeeeef

**Sample Output 02 :**

    90


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#

def solve(s):
    # Write your code here



Editorial: 20/20 Hack July Editorial: Game of Thrones - 2
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p><a href="https://www.hackerrank.com/contests/july13/challenges/game-of-throne-ii">Game Of Thrones - II</a>  </p>

<p><strong>Problem:</strong> Given a string such that at least one of anagram of string is palindrome. Tell how many anagram of given string will be palindrome.</p>

<p><strong>Difficulty level:</strong> Easy <br>
<strong>Required Knowledge:</strong> Permutations,  Modular Inverse <br>
<strong>Time Complexity:</strong> O(N)  </p>

<p><strong>Approach:</strong> Given a string tell how many anagram of given string will be palindrome. So  we can consider a sub-string which we can get by taking half of number of each character. length of this sub-string will    L/2 where L is length of original string. we can get permutation of given string then we can concatenate the reverse of given half. so get anagram of given string which will be palindrome.So, we can use following <a href="http://www.regentsprep.org/Regents/math/ALGEBRA/APR2/LpermRep.htm">formula</a> to get number of permutation of given first half. </p>

<p>So , as answer can be very huge . But as in question we are asked for number of permutations which are palindrome % 10^9+7 and we know 10^9 + 7 is a prime number . So, we can get it using modular inverse techniques.</p>

<p><strong>Problem Setter's code:</strong></p>

<pre><code>#!/usr/bin/py
from collections import Counter

mod = 10**9 + 7

def hasPalindrome(word):
    x = Counter(word)
    count = 0
    for i in x:
        if x[i]%2!=0:
            count += 1

    if count &gt; 1:
        return False
    else:
        return True

def getfactmod(b):
    val = 1
    for i in range(1,b):
        val =((val%mod)*((i+1)%mod))%mod

    return val

def getpowermod(a,b):
    if b == 0:
        return 1
    if b == 1:
        return a
    temp = getpowermod(a,b/2)
    if b%2==0:
        return ((temp%mod)**2)%mod
    else:
        return (a*((temp%mod)**2))%mod

def inversemod(a):
    return getpowermod(a,mod-2)

if __name__ == '__main__':
    word = raw_input()
    assert 1 &lt;= len(word) &lt;= 10**5
    assert hasPalindrome(word) == True
    counter = Counter(word)
    denominator = 1
    numerator = 0
    for i in counter:
        numerator += counter[i]/2
        denominator = ((denominator%mod)*(getfactmod(counter[i]/2)%mod))%mod

    numerator = getfactmod(numerator)
    answer = ((numerator%mod)*(inversemod(denominator)%mod))%mod
    print answer
</code></pre>

<p><strong>Tester's code:</strong></p>

<pre><code>#include &lt;cstdio&gt;
#include &lt;cstring&gt;
#include &lt;bits/stdc++.h&gt;

using namespace std;
int t;
const long long mod = 1000000007;

long long pow(long long osn){
    long long res = 1, tmp = osn;
    int m = int(mod - 2);
    while(m){
        if(m % 2){
            res = (res * tmp) % mod;
            --m;
        }
        m &gt;&gt;= 1;
        tmp = (tmp * tmp) % mod;
    }
    return res;
}

long long fact(int num){
    long long res(1), curp(num);
    while(curp)res = (res * curp--) % mod;
    return res;
}

int obr(){
    int num[256];
    memset(num, 0, sizeof(num));
    char s[1001];
    cin&gt;&gt;s;
    int n = strlen(s);
    for(int i = 0; i &lt; n; ++i)++num[s[i]];
    for(int i=0 ; i&lt;256 ; i++)
        num[i] = num[i]/2;
    n = n/2;
    long long res = fact(n);
    for(int i = 'a'; i &lt;= 'z'; ++i)
        if(num[i])res = (res * pow(fact(num[i]))) % mod;
    for(int i = 'A'; i &lt;= 'Z'; ++i)
        if(num[i])res = (res * pow(fact(num[i]))) % mod;
    int topr = int(res);
    printf("%d\n", topr);
    return 0;
}

int main(){
    t = 1;
    for(int i = 0; i &lt; t; ++i)
        obr();
    return 0;
}
</code></pre>

Reference solution (C++):
###C++  
```cpp  
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
    
using namespace std;
int t;
const long long mod = 1000000007;
    
long long pow(long long osn){
    long long res = 1, tmp = osn;
    int m = int(mod - 2);
    while(m){
        if(m % 2){
            res = (res * tmp) % mod;
            --m;
        }
        m >>= 1;
        tmp = (tmp * tmp) % mod;
    }
    return res;
}
    
long long fact(int num){
    long long res(1), curp(num);
    while(curp)res = (res * curp--) % mod;
    return res;
}
    
int obr(){
    int num[256];
    memset(num, 0, sizeof(num));
    char s[1001];
    cin>>s;
    int n = strlen(s);
    for(int i = 0; i < n; ++i)++num[s[i]];
    for(int i=0 ; i<256 ; i++)
        num[i] = num[i]/2;
    n = n/2;
    long long res = fact(n);
    for(int i = 'a'; i <= 'z'; ++i)
        if(num[i])res = (res * pow(fact(num[i]))) % mod;
    for(int i = 'A'; i <= 'Z'; ++i)
        if(num[i])res = (res * pow(fact(num[i]))) % mod;
    int topr = int(res);
    printf("%d\n", topr);
    return 0;
}
    
int main(){
    t = 1;
    for(int i = 0; i < t; ++i)
        obr();
    return 0;
}
```