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 Haskell (GHC 7.10.3) (judge verdict: Accepted)


import qualified Data.Vector.Unboxed as V
import Data.Array.IArray (listArray, accumArray, (!))
import Data.Array.MArray
import Data.Array.ST (STUArray, runSTUArray)
import Data.Array.Unboxed (UArray)
import Data.List (sortOn, sortBy, unfoldr, partition)
import Data.Char (isSpace)
import Control.Monad.ST (ST, runST)
import Control.Monad.Trans.State (StateT(..))
import qualified Data.ByteString.Char8 as B
import qualified  Data.Set as S
import Control.Arrow (first)

import Debug.Trace (trace)

-- utility functions

--tr x = trace (show x) x
tr = id

--   Input functions
readInt = B.readInt . B.dropWhile isSpace

readInts :: Int -> B.ByteString -> [Int]
readInts n = take n . unfoldr readInt

readIntsV :: Int -> B.ByteString -> V.Vector Int
readIntsV n = V.unfoldrN n readInt

readIntsA :: Int -> B.ByteString -> UArray Int Int
readIntsA n bs = runSTUArray $ do
  arr <- newArray_ (0,n-1)
  go arr 0 bs
  return arr
  where
    go :: STUArray s Int Int -> Int -> B.ByteString -> ST s ()
    go arr i bs
      | i==n = return ()
      | otherwise = do
        let Just (x,bs') = readInt bs
        writeArray arr i x
        go arr (i+1) bs'

readTsV :: Int -> B.ByteString -> V.Vector (Int,Int)
readTsV n = V.unfoldrN n $ runStateT ((,) <$> StateT readInt <*> StateT readInt)

readTs :: Int -> B.ByteString -> [(Int,Int)]
readTs n = take n . unfoldr readT where
  readT = runStateT ((,) <$> StateT readInt <*> StateT readInt)

-- main

modular = 10^9 + 7

count "" arr = return ()
count (x:xs) arr = do
  c <- readArray arr x
  writeArray arr x (c+1)
  count xs arr

sub x arr = readArray arr x >>= writeArray arr x . pred

productm m [] = 1
productm m (x:xs) = (x * productm m xs) `mod` m

solv s = runST $ do
  alphanum <- newArray ('a','z') 0 :: ST s (STUArray s Char Int)
  count s alphanum
  go s alphanum
  where
    p c v = fst v/=c && snd v > 0
    go "" alphanum = return 0
    go (x:xs) alphanum = do
      sub x alphanum
      counts <- map snd . filter (p x) <$> getAssocs alphanum
      r <- go xs alphanum
      let choice = productm modular $  map succ counts
      return $ (r + choice) `mod` modular

main = do
  getLine
  s <- getLine
  print $ solv s