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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        InputStreamScanner in = new InputStreamScanner(System.in);
        new Main().solve(in, out);
        out.flush();
    }

    private void solve(InputStreamScanner in, PrintWriter out) {
        int n = in.nextInt();
        char[] s = in.next().toCharArray();

        int[] c = new int[26];
        for (int i = 0; i < n; i++) {
            c[s[i] - 'a']++;
        }

        List<Integer> l = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            if (c[i] != 0) {
                l.add(c[i]);
            }
        }

        int[] x = new int[l.size()];
        int z = 0;
        for (int i = 0; i < l.size(); i++) {
            x[i] = l.get(i);
            z += x[i];
        }

        ArrayDeque<Tree> q = new ArrayDeque<>();
        for (int i = 0; i < x.length; i++) {
            q.add(new Tree(x[i], i));
        }

        int m = 0;
        while (!q.isEmpty()) {
            Tree t = q.pop();

            if (t.d >= x.length) {
                continue;
            }

            for (int i = t.d + 1; i < x.length; i++) {
                int o = (t.c * x[i]) % 1000000007;
                m = (m + o) % 1000000007;
                q.add(new Tree(o, i));
            }

        }

        out.println(m + z);
    }

    static class Tree {
        int c;
        int d;

        Tree (int c, int d) {
            this.c = c;
            this.d = d;
        }
    }

    static class InputStreamScanner {

        private InputStream in;

        private byte[] buf = new byte[1024];
        private int len = 0;
        private int off = 0;

        InputStreamScanner(InputStream in)	{
            this.in = in;
        }

        String next() {
            StringBuilder sb = new StringBuilder();
            for (int b = skip(); !isSpace(b);){
                sb.appendCodePoint(b);
                b = read();
            }
            return sb.toString();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        char nextChar() {
            return (char)skip();
        }

        int skip() {
            for (int b; (b = read()) != -1;) {
                if (!isSpace(b)) {
                    return b;
                }
            }
            return -1;
        }

        private boolean isSpace(int c) {
            return c < 33 || c > 126;
        }

        private int read() {
            if (len == -1) {
                throw new InputMismatchException("End of Input");
            }
            if (off >= len){
                off = 0;
                try {
                    len = in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException(e.getMessage());
                }
                if (len <= 0) {
                    return -1;
                }
            }
            return buf[off++];
        }
    }
}