Problem:
Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>

typedef unsigned long ulong;

ulong diff(ulong a, ulong b){
    return __builtin_ctzl(a^b);
}

ulong hd(ulong a, ulong b){
    return __builtin_popcountl(a^b);
}

ulong bit_remove(ulong a, ulong n){
    ulong low  = a & ((1<<n)-1);
    ulong high = (a >> (n+1)) << n;
    return high | low;
}

ulong bit_insert(ulong a, ulong n, ulong b){
    ulong low  = a & ((1<<n)-1);
    ulong high = (a >> n) << (n+1);
    return high | (b<<n) | low;
}
#define N 22
long data[1<<N];

void solve(const long n, const long a, const long b, const size_t sp, const size_t ep, const long mask){
    if(n==1){
        data[sp+0] = a & mask;
        data[sp+1] = b & mask;
        return;
    }
    ulong d = diff(a&mask, b&mask);
    ulong e = __builtin_ctzl(mask);
    ulong aa = a;//bit_remove(a, d);
    ulong ab = a ^ (1<<e);//bit_remove(a, d) ^ 1;
    ulong bb = b;//bit_remove(b, d);
    const size_t mp = (sp+ep)/2;
    solve(n-1, aa, ab, sp, mp, mask ^ (1<<d));
    solve(n-1, ab, bb, mp, ep, mask ^ (1<<d));
    for(size_t i=sp; i<mp; i++){
        //data[i] = bit_insert(data[i], d, (a&(1<<d)) >> d);
        data[i] |= a&(1<<d);
    }
    for(size_t i=mp; i<ep; i++){
        //data[i] = bit_insert(data[i], d, (b&(1<<d)) >> d);
        data[i] |= b&(1<<d);
    }
}

int main(void){
    long n, a, b;
    scanf("%ld %ld %ld", &n, &a, &b);
    if(__builtin_popcountl(a)%2 == __builtin_popcountl(b)%2){
        printf("NO\n");
        return 0;
    }
    solve(n, a, b, 0, 1<<n, 0xFFFFFFFFFFFFFFFF);
    const size_t m = 1<<n;
    printf("YES\n");
    for(size_t i=0; i<m-1; i++){
        printf("%ld ", data[i]);
    }
    printf("%ld\n", data[m-1]);
    return 0;
}