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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace AGC031A
{
    class Program
    {
        static void Solve()
        {
            var n = Input.NextInt();
            var s = Input.Next();

            var map = s.GroupBy(x => x)
                .ToDictionary(x => x.Key, x => x.Count());

            var m = new ModInt(1);
            foreach (var item in map)
            {
                m *= item.Value + 1;
            }

            Console.WriteLine(m - 1);
        }

        /// <summary>
        /// Mを法とする数値
        /// </summary>
        struct ModInt
        {
            public static int M { get; private set; }
            private static bool mIsPrime;
            private static int[] factorials;
            private static int[] invs;
            private static int[] finvs;

            private readonly int N;

            static ModInt()
            {
                ModInt.Init();
            }

            public static void Init(int mod = (int)1e9 + 7)
            {
                ModInt.M = mod;
                ModInt.mIsPrime = IsPrime(mod);

                ModInt.factorials = Enumerable.Repeat<int>(1, 2).ToArray();
                ModInt.invs = Enumerable.Repeat<int>(1, 2).ToArray();
                ModInt.finvs = Enumerable.Repeat<int>(1, 2).ToArray();
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public ModInt(int n)
            {
                this.N = n % M;
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static implicit operator int(ModInt a)
                => a.N;

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static implicit operator ModInt(int a)
                => new ModInt(a);

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static int Add(int a, int b)
                => (a + b) % M;

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static int Sub(int a, int b)
                => (a + M - b) % M;

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static int Mul(int a, int b)
                => (int)(((long)(a % M) * (b % M)) % M);


            /// <summary>
            /// aの逆元（a^-1）
            /// Mとaが互いに素である必要があるが
            /// 大抵Mが素数でa<Mなのでチェックしない
            /// </summary>
            /// <param name="a"></param>
            /// <returns></returns>
            public static int Inverse(int a)
            {
                int b = M;
                int u = 1, v = 0;

                while (b > 0)
                {
                    int t = a / b;
                    a -= t * b;
                    Swap(ref a, ref b);
                    u -= t * v;
                    Swap(ref u, ref v);
                }

                u %= M;
                if (u < 0) u += M;

                return u;
            }

            /// <summary>
            /// a/b
            /// </summary>
            /// <param name="a"></param>
            /// <param name="b"></param>
            /// <returns></returns>
            public static int Div(int a, int b)
            {
                // 厳密には条件違うけど、逆元を求めさせるときは大抵Mは素数なので……
                if (!mIsPrime)
                {
                    throw new ArgumentException();
                }

                return Mul(a, Inverse(b));
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator +(ModInt a, ModInt b)
                => new ModInt(Add(a.N, b.N));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator -(ModInt a, ModInt b)
                => new ModInt(Sub(a.N, b.N));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator *(ModInt a, ModInt b)
                => new ModInt(Mul(a.N, b.N));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator /(ModInt a, ModInt b)
                => new ModInt(Div(a.N, b.N));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator +(ModInt a, int b)
                => new ModInt(Add(a.N, b));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator -(ModInt a, int b)
                => new ModInt(Sub(a.N, b));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator *(ModInt a, int b)
                => new ModInt(Mul(a.N, b));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt operator /(ModInt a, int b)
                => new ModInt(Div(a.N, b));

            public override string ToString()
            {
                return this.N.ToString();
            }

            /// <summary>
            /// x^y
            /// 繰り返し二乗法（バイナリ）
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public static ModInt Power(int x, int y)
            {
                var r = 1;

                while (y > 0)
                {
                    if ((y & 1) != 0)
                    {
                        r = Mul(r, x);
                    }
                    x = Mul(x, x);

                    y >>= 1;
                }

                return new ModInt(r);
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public static ModInt Power(ModInt x, ModInt y)
                => new ModInt(Power(x.N, y.N));

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            public ModInt Power(ModInt y)
                => new ModInt(Power(this, y));

            #region nCr
            /// <summary>
            /// a以下の階乗のテーブルを作成する
            /// </summary>
            /// <param name="a"></param>
            static void FactTable(int a)
            {
                if (a >= factorials.Length)
                {
                    int begin = factorials.Length;

                    Array.Resize(ref factorials, a + 1);

                    for (int i = begin; i <= a; i++)
                    {
                        factorials[i] = Mul(factorials[i - 1], i);
                    }
                }
            }

            /// <summary>
            /// a以下の逆元テーブルを作成する
            /// </summary>
            /// <param name="a"></param>
            static void InvTable(int a)
            {
                if (a >= invs.Length)
                {
                    int begin = invs.Length;

                    Array.Resize(ref invs, a + 1);

                    for (int i = begin; i <= a; i++)
                    {
                        invs[i] = M - Mul(invs[M % i], M / i);
                    }
                }
            }

            /// <summary>
            /// a以下の階乗の逆元テーブルを作成する
            /// </summary>
            /// <param name="a"></param>
            static void FinvTable(int a)
            {
                if (a >= finvs.Length)
                {
                    int begin = finvs.Length;

                    Array.Resize(ref finvs, a + 1);

                    for (int i = begin; i <= a; i++)
                    {
                        finvs[i] = Mul(finvs[i - 1], invs[i]);
                    }
                }
            }

            /// <summary>
            /// nCr
            /// </summary>
            /// <param name="n"></param>
            /// <param name="r"></param>
            /// <returns></returns>
            public static int Ncr(int n, int r)
            {
                if (n == r) return 1;

                if (factorials.Length <= n) FactTable(n);
                if (invs.Length <= n) InvTable(n);
                if (finvs.Length <= n) FinvTable(n);

                return Mul(factorials[n], Mul(finvs[r], finvs[n - r]));
            }
            #endregion

            #region Misc
            /// <summary>
            /// 素数か
            /// </summary>
            /// <param name="x"></param>
            /// <returns></returns>
            static bool IsPrime(long x)
            {
                if (x < 2)
                {
                    return false;
                }
                if (x == 2)
                {
                    return true;
                }
                if ((x % 2) == 0)
                {
                    return false;
                }

                long rx = (long)(Math.Sqrt(x) + 1);
                for (long n = 3; n < rx; n += 2)
                {
                    if ((x % n) == 0)
                    {
                        return false;
                    }
                }

                return true;
            }

            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
            static void Swap<T>(ref T a, ref T b)
            {
                T temp = a;
                a = b;
                b = temp;
            }
            #endregion
        }

        #region Competitive Template
        public static void Main(string[] args)
        {
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 仮想的に標準入力をセットする
            // NextLine系を使っていると使えない
            //Input.SetText("");

            Solve();

            Console.Out.Flush();
        }

        static class Input
        {
            static char[] separator = { ' ' };
            public static bool IsEof { get; set; }
            static Queue<string> q { get; set; }

            static Input()
            {
                IsEof = false;
                q = new Queue<string>();
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="items"></param>
            public static void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                {
                    SetText(item);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static bool SetText(string s)
            {
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                return true;
            }

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            /// <returns></returns>
            static bool read()
            {
                var s = Console.ReadLine();
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                if (!q.Any()) return read();

                return true;
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            /// <returns></returns>
            public static string Next()
            {
                if (!q.Any())
                {
                    if (!read())
                    {
                        IsEof = true;
                        return "";
                    }
                }

                return q.Dequeue();
            }

            public static int NextInt() => int.Parse(Next());
            public static long NextLong() => long.Parse(Next());
            public static double NextDouble() => double.Parse(Next());

            public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList();
            public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList();
            public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList();
            public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList();

            public static List<string> NextLine() => Console.ReadLine().Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
            public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList();
            public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList();
            public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList();
        }
        #endregion
    }
}