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



Sample input:
4
abcd


Sample output:
15

Submission status: Time Limit Exceeded

Solution code:
import itertools
from collections import Counter
from functools import reduce
from operator import add, mul

n = int(input())
l = list(input())
l = list(Counter(l).values())

ans = n

for i in range(2, len(l)+1):
    conbis = list(itertools.combinations(l, i))
    for conbi in conbis:
        ans += reduce(mul, conbi)

print(ans%(10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import itertools
from collections import Counter
import numpy as np

n = int(input())
l = list(input())
l = list(Counter(l).values())

ans = n

for i in range(2, len(l)+1):
    conbis = list(itertools.combinations(l, i))
    for conbi in conbis:
        ans += np.prod(conbi)

print(ans%(10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())

import itertools

def has_duplicates(seq):
    return len(seq) != len(set(seq))

cnt=0
tmp=[]
for i in range(1,N+1):
    c=itertools.combinations(S,i)
    for v in c:
        tmp.append(v)

for i in tmp:
    if has_duplicates(i):
        cnt+=1

print(len(tmp)-cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
p = 10**9+7
N = int(input())
S = input().strip()
C = {}
for i in range(N):
    if S[i] not in C:
        C[S[i]]=0
    C[S[i]] += 1
D = list(C.items())
n = len(D)
cnt = 0
for i in range(1,n+1):
    for x in combinations(range(n),i):
        prod = 1
        for j in x:
            prod = (prod*D[j][1])%p
        cnt = (cnt+prod)%p
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int
from math import factorial
from collections import Counter
def solve(N: int, S: str):
    counter = dict(Counter(S))
    values = list(counter.values())
    LEN = len(counter)
    answer = 0        
    for i in range(1,2**LEN):
        b = format(i,'b').zfill(LEN)
        an = 1
        for j in range(LEN):
            if b[j] == '1':
                an *= values[j]
        answer += an
        answer %= MOD

    print(answer)


    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
S = input()

mod = 10 ** 9 + 7
s = list(Counter(S).values())

ans = 0 
for i in range(1, 1 << len(s)):
    k = 1
    for j in range(len(s)):
        if (i >> j) & 1 == 1:
            k *= s[j]
            k %= mod
    ans += k
    ans %= mod
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
if __name__ == '__main__':
    n=int(input())
    string=input()
    string_set=set(string)
    div=10**9+7
    count=0
    Dict={}
    for i,s in enumerate(string):
        if s not in Dict.keys():
            Dict[s]=1
        else:
            Dict[s] += 1
        count+=1
    if len(string_set)>1:
        for i in range(2,len(string_set)+1):
            for x in itertools.combinations(Dict.keys(),i):
                temp = 1
                for y in x:
                    temp*=Dict[y]
                count+=temp
    print(count%div)

Submission status: Time Limit Exceeded

Solution code:
import itertools
if __name__ == '__main__':
    n=int(input())
    string=input()
    string=list(string)
    string_set=set(string)
    div=10**9+7
    count=0
    Dict={}
    for i,s in enumerate(string):
        if s not in Dict.keys():
            Dict[s]=1
        else:
            Dict[s] += 1
        count+=1

    if len(string_set)>1:
        for i in range(2,len(string_set)+1):
            for x in itertools.combinations(Dict.keys(),i):
                temp = 1
                for y in x:
                    temp*=Dict[y]
                count+=temp

    print(count%div)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def one_hot(n):
    temp_0=[0]
    temp_1=[1]
    ret=[]
    S=[temp_0,temp_1]
    v=S.pop()
    while len(ret)!=2**n:
        if len(v)==n:
            ret.append(v)
            if len(S)!=0:
                v = S.pop()
        else:
            S.append(v+[0])
            S.append(v+[1])
            v=S.pop()
    return ret

if __name__ == '__main__':
    n=int(input())
    string=input()
    string=list(string)
    div=10**9+7
    count=0
    iteration=one_hot(n)
    for i in iteration:
        temp=[]
        for s,j in zip(string,i):
            if j==1:
                temp.append(s)
        if len(temp)!=0 and len(temp)==len(set(temp)):
            count+=1
    print(count%div)

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
n = int(input())
s = list(input())
count = collections.Counter(s).most_common()

ans = 0
for p in itertools.product([0,1],repeat=len(count)):
  tmp =1
  for i,x in enumerate(p):
    tmp = tmp*count[i][1]**x
  ans += tmp
print((ans-1)%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c

n = int(input())
s = input()

ans = 0
for i in range(n): # i+1文字取り出す場合
    num = i+1
    l = list(c(s, num))
    for j in l:
        if len(j) == len(set(j)):
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import combinations
N = int(input())
S = input()
MOD = 10**9 + 7

C = Counter(S)
ans = 0
for i in range(1, min(N+1, 27)):
    for prod in combinations(C.values(), i):
        a = 1
        for p in prod:
            a *= p
            a %= MOD
        ans += a
        ans %= MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import combinations
N = int(input())
S = input()
MOD = 10**9 + 7

C = Counter(S)
ans = 0
for i in range(1, N+1):
    for prod in combinations(C.values(), i):
        a = 1
        for p in prod:
            a *= p
            a %= MOD
        ans += a
        ans %= MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
s=str(input())
a=Counter(s)
aa=list(a.values())
aa.sort()
aa=aa[::-1]
m=len(aa)
ans=0
#print(aa)
for i in range(1,2**m):
    z=format(i,'b').zfill(m)
    #print(z)
    ans1=1
    for j in range(m):
        if aa[j]==1:
            ans+=ans1%(10**9+7)
            break
        if int(z[j])>0:
            ans1*=aa[j]*int(z[j])%(10**9+7)
    else:
        ans+=ans1%(10**9+7)
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import product
n=int(input())
s=str(input())
a=Counter(s)
aa=list(a.values())
aa.sort()
aa=aa[::-1]
m=len(aa)
ans=0
#print(aa)
for i in range(1,2**m):
    z=format(i,'b').zfill(m)
    #print(z)
    ans1=1
    for j in range(m):
        if int(z[j])>0:
            ans1*=aa[j]*int(z[j])%(10**9+7)
    ans+=ans1%(10**9+7)
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ch = {}
for c in s:
  if c in ch:
    ch[c] += 1
  else:
    ch[c] = 1

ans = 1

for c in ch:
  if ch[c] != 0:
    ans *= 1 + ch[c]
  else:
    continue
  if ans >= 1000000007:
    ans -= 1000000007

ans += 1000000007
ans -= 1
while ans >= 1000000007:
  ans -= 1000000007
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
import itertools
for i in range(1,n+1):
    combs=list(itertools.combinations(s,i))
    for comb in combs:
        if len(comb)==len(set(comb)):
            count+=1
print(count%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = [s for s in S]
import itertools
ans = 0
for i in range(1, N+1):
    for c in itertools.combinations(S,i):
        if len(c) == len(set(c)):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

ans = 0
# 文字列 s2 の末尾までを走査して、
# s1 にまだ表れていない文字があれば連結して、
# 連結後の s1 と、s2 から走査済みの文字を除いたものを返す
def func(s1, s2):
    global ans
    if s2 == "":
        return None
    for i,c in enumerate(s2):
        if c not in s1:
            ans += 1
            t1 = s1 + c
            t2 = s2[i+1:]
#            print(t1, t2, ans)
            func(t1, t2)

N = int(input())
S = input().strip()

func("", S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = input()
s = input()
 
ref = ["0"]
 
for i in range(len(s)):
    for j in range(len(ref)):
        if s[i] not in ref[j]:
            ref.append(ref[j]+s[i])
 
print(len(ref)-1)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

N=int(input())
S=input()
INF=1e9+7
dict=defaultdict(int)
for i in S:
    dict[i]+=1
l=list(dict.values())
ans=0
for i in range(1,2**len(l)):
    i=bin(i)[2:].zfill(len(l))
    tmp=1
    for j in range(len(l)):
        if i[j]=="1":
            tmp*=l[j]
            tmp%=INF
    ans+=tmp
    ans%=INF
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import itertools
import functools
import operator

def main():
    num = int(input())
    data = list(input())

    count_dic = defaultdict(int)

    for i in range(num):
        count_dic[data[i]] += 1

    count_list = list(count_dic.values())

    ans = 0
    for i in range(1, len(count_list) + 1):
        now_data = list(itertools.combinations(count_list, i))
        for ele in now_data:
            result = functools.reduce(operator.mul, ele)
            ans += result
    mod = 10 ** 9 + 7
    print(ans % mod)





if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
S = input()

count = -1

def dfs(index, moji):
    global N,S,count
    if index==N:
        count+=1
        return
    if S[index] in moji:
        dfs(index+1, moji)
    else:
        dfs(index+1, moji)
        dfs(index+1, moji+S[index])

dfs(0,'')
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
s = input()
 
x = []
 
for i in range(n):
    x.append(s[i])
 
m = len(set(x))
 
import itertools
 
cnt = 0
for i in range(1, m+1):
    p = list(itertools.combinations(s,i))
    cnt += sum(len(pp) == len(set(pp)) for pp in p)
 
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import itertools
dellist = lambda items, indexes: [item for index, item in enumerate(items) if index not in indexes]


N = input()
S = input()

N = int(N)
S = list(S)

outcome = []
indexlist = []
for length in range(1, N+1):
    indexlist2 = list(itertools.combinations(range(N),N-length))
    for i in range(len(indexlist2)):
        indexlist.append(list(indexlist2[i]))
    for i in range(len(indexlist)):       
        outcome.append([])
        outcome[i] = dellist(S, list(indexlist[i]))
        
outcome = [x for x in outcome if x]


delindex = []
for i in range(len(outcome)):
    for j in range(len(outcome[i])):
        if outcome[i].count(outcome[i][j]) > 1:
            delindex.append(i)
delindex = set(delindex)

outcome = dellist(outcome, delindex)

print(len(outcome))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()


def func(string, index):
    num = 1
    if index == N:
        return num

    for i in range(index, N):
        if S[i] not in string:
            num += func(string + S[i], i + 1)

    return num


ans = func('', 0) - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def counter(flag,st):
  count = 0
  if st == '':
    return count
  for i in range(len(st)):
    if not st[i] in flag:
      flag+=st[i]
      count += 1
      if count > 10**9:
        count%=10**9+7
      if i < len(st)-1:
        count += counter(flag,st[i+1:])
      flag.pop(-1)
  return count

N = int(input())
S = input()

cnt = counter([],S)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
MAX = 26

def has_same_chr(s):
    for i in range(0, len(s)):
        for j in range(i+1, len(s)):
            if s[i] == s[j]:
                #print("{}, {}:{}".format(i,j,s[i]))
                return False
    return True

# 長さlengthの部分文字列をsから取得してs_retに付与してs_listに追加して返す
def get_partial_strings(length, s_ret, s_list, s):
    if length == 0:
        #print("s_ret:{}".format(s_ret))
        return s_ret
    
    for i in range(0, len(s)):
        new_ret = s_ret + s[i]
        #print("new:{}, s:{}".format(new_ret, s[i+1:]))
        s_list.append(get_partial_strings(length-1, new_ret, s_list, s[i+1:]))
    
    return s_list
    

l=int(input())
s=input()

ret = 0
max = MAX
if l < MAX:
    max = l
for i in range(1,l+1):
    strs = []
    for string in get_partial_strings(i, "", strs, s):
        if has_same_chr(string):
            #print(string)
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit

N = int(input())
S = input()


def get_count():
    count = 1

    considered = []

    for s in S:
        if s not in considered:
            count *= S.count(s) + 1
        considered.append(s)

    count -= 1

    return count


ans = get_count() % (10 ** 9 + 7)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

dic = {}
for s_i in s:
	dic[s_i] = s.count(s_i)+1

p = 10**9+7
res = 1
for v in dic.values():
	res = (res*v)%p

print((res-1)%p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

dic = {}
for s_i in s:
	dic[s_i] = s.count(s_i)+1

res = 1
for v in dic.values():
	res = res*v

p = 10**9+7
print((res-1)%p)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
S = list(input())
count = 0

def is_unique(seq):
    return len(seq) == len(set(seq))


for i in range(N):
    results = list(itertools.combinations(S,i+1))
    
    for result in results:
        if is_unique(result):
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
S=input()
ans=N
for i in range(2, N+1):
  sub=[x for x in list(itertools.combinations(S, i)) if len(x) == len(set(x))]
  ans+=len(sub)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)

n = int(input())
s = input()
s_set = set(s)
S = []
for i in s_set:
    S.append(s.count(i))

def cal(n, S):
    if n == 1:
        return sum(S)
    else:
        ans = 0
        for i in range(len(S) - n + 1):
            cross = cal(n-1, S[i+1:])
            ans += S[i] * cross
            ans %= 10 ** 9 + 7
        return ans % (10 ** 9 + 7)

ans = 0
for i in range(n):
    ans += cal(i+1, S)

ans %= 10 ** 9 + 7
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools as it
n = int(input())
s = input()
s_dic = {}
for c in s:
    if not c in s_dic.keys():s_dic[c] = 1
    else:s_dic[c] += 1
#print(s_dic)
ndic = {}
for i in range(1,len(s_dic)+1):
    a = it.combinations(s_dic.keys(), i)
    for t in a:
        key = ''.join((t))
        #print(key)
        if len(key) == 1:
            #print('one word key')
            ndic[key] = s_dic[key]
        else:
            #print('keys:)',key[:-1],key[-1])
            #print('value:',ndic[key[:-1]],ndic[key[-1]])
            ndic[key] = (ndic[key[:-1]] * ndic[key[-1]]) % (10**9+7)
#print(ndic)
print(sum(ndic.values()) % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import itertools as it
#s = 'abcab'
n = int(input())
s = input()
s_dic = {}
for c in s:
    if not c in s_dic.keys():s_dic[c] = 1
    else:s_dic[c] += 1
#print(s_dic)
g = 0
for i in range(1,len(s_dic)+1):
    a = it.combinations(s_dic.keys(), i)
    #print('lis:',list(a))
    gt = 0
    for t in a:
        #print('combi')
        gi = 1
        for u in list(t):
            #print('u;',u)
            gi *= s_dic[u]
        #print(gi)
        gt += (gi % (10**9+7))
        #gi *= s_dic[t]
    #print(gt)
    g += gt
print(g% (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    N = int(input())
    S = input()

    str_list = list()
    for s in S:
        str_list.append(s)

    str_uni_list = {}
    for str in str_list:
        if (str in str_uni_list) is False:
            str_uni_list[str] = 1
        else:
            str_uni_list[str] += 1

    list_val = []
    for i in str_uni_list.values():
        list_val.append(i)

    ans = 0
    temp = 1
    for i in range(1, len(list_val) + 1):
        for j in list(itertools.combinations(list_val, i)):
            for k in j:
                temp *= k
            ans += temp
            temp = 1

    print(ans % (10 ** 9 + 7))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    N_l = len(N)
    global temp_ans
    for i in range(N_l)[::-1]:
        temp_ans += N[i]
        #print(temp_ans)
        for s in temp_ans:
            #print(s)
            if N[i] not in s:
                temp_ans.append(N[i]+s)
    #print(temp_ans)
    return len(temp_ans) % (10**9 + 7)


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

#ans = []
#temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    N_l = len(N)
    global temp_ans
    #global ans
    #global temp_array
    for i in range(N_l)[::-1]:
        temp_ans += N[i]
        #if i == N_l-1:
        #    temp_ans = ans
        #else:
        for s in temp_ans:
            if s[0] != N[i] and N[i] not in s:
                temp_ans.append(N[i]+s)
                    #print(N[i]+s)
        #print(temp_ans)
        #ans += temp_array
        #temp_ans = ans
        #temp_array = []
    return len(temp_ans) % (10**9 + 7)


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

ans = []
temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    N_l = len(N)
    global temp_ans
    global ans
    global temp_array
    for i in range(N_l)[::-1]:
        ans += N[i]
        if i == N_l-1:
            temp_ans = ans
        else:
            for s in temp_ans:
                if s[0] != N[i] and N[i] not in s:
                    temp_ans.append(N[i]+s)
                    #print(N[i]+s)
        #print(temp_ans)
        ans += temp_array
        temp_ans = ans
        temp_array = []
    return len(ans) % (10**9 + 7)


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

ans = []
temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    N_l = len(N)
    global temp_ans
    global ans
    global temp_array
    for i in range(N_l)[::-1]:
        ans += N[i]
        if i == -N_l+1:
            temp_ans = ans
        if len(temp_ans) != 0:
            temp_ans += [N[i]+s for s in temp_ans if s[0] != N[i] and N[i] not in s]
        ans += temp_array
        temp_ans = ans
        temp_array = []
    return len(ans) % (10**9 + 7)


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
import functools
import operator
prod = functools.partial(functools.reduce, operator.mul)

N = int(input())
S = list(input())  # len:N

S = sorted(S)  #  アルファベット順に並べ替え
U = [0] * 26  #  ユニーク数数え上げ
tmp = "0"
count = 0
u_count = 0
for i in range(N):
    if i > 0:
        if tmp == S[i]:
            U[count] += 1
        else:
            tmp = S[i]
            count += 1
            U[count] += 1
    else:
        tmp = S[i]
        U[count] += 1


for u in range(26):
    if U[u] != 0:
        u_count += 1

U = sorted(U)

for i in range(len(U)):
    if U[0] == 0:
        U.pop(0)

count = 0

for i in range(1, u_count+1):
    count += np.sum(np.array(list(itertools.combinations(U, i))).prod(axis=1))

a = 10 ** 9 + 7
print(count % a)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
s = input()
cnt = 0
for i in range(n)[::-1]:
  ch = s[i:]
  c = collections.Counter(ch)
  tmp_cnt = 1
  for k,v in c.items():
    if k != s[i]:
      tmp_cnt *= (v+1)
  cnt += tmp_cnt % (10**9+7)
print(cnt% (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
from itertools import*;n=int(input());i=input();print(sum(len(f)==len(set(f))for g in[combinations(i,r)for r in range(1,n+1)]for f in g))

Submission status: Time Limit Exceeded

Solution code:
from itertools import*;input();i=input();l=[]
for r in range(1,len(i)+1):l.extend(combinations(i,r))
print(sum(len(s)==len(set(s))for s in l))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
#import sys
#sys.setrecursionlimit(10**7)

ans = []
temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    #print(S, N)

    #% (10^9 + 7)
    #return count_0
    N_l = len(N)
    #print(range(N_l)[::-1])
    #print(range(N_l))
    #global ans 
    #ans.append(N[-1])
    global temp_ans
    global ans
    for i in range(N_l)[::-1]:
    #for i in range(N_l):
        ans += N[i]
        if i == -N_l+1:
            temp_ans = ans
        b(N, N[i], i)
        #global temp_ans
        global temp_array
        ans += temp_array
        temp_ans = ans
        #print(temp_ans)
        #print(temp_array)
        temp_array = []
        #print(ans)
    #print(list(set(ans)))
    #print(10**9 + 7)

    # 2
    #return len(list(set(ans))) % (10**9 + 7)
    #print(ans)
    return len(ans) % (10**9 + 7)


def b(org_array, temp, i):
    global temp_array
    global temp_ans
    """
    if len(org_array) == 0:
        #global temp_array
        #global ans
        #ans += temp_array
        #print(temp_array)
        #temp_array = []
        return
    else:
        #print("org array:"+org_array)
        #ans.append(temp)
        #print(temp)
        #print(temp_array)
    """
    if len(temp_ans) != 0:
        temp_ans += [temp+s for s in temp_ans if s[0] != temp and temp not in s]

        #for tmp in temp_array:
        #    b(org_array, tmp, i+1)
        """
        if temp not in temp_array:
            temp_array.append(temp)
        new_temp = temp
        b(org_array, new_temp, i+1)
        if org_array[i+1] not in temp:
            temp += org_array[i+1]
            if temp not in temp_array:
                temp_array.append(temp)
            b(org_array, temp, i+1)
        #print("org: "+org_array[i+1])
        #print(temp)
        #b(org_array, temp, i+1)
        """


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import product
 
N = int(input())
S = input().strip()
dic = Counter(S)
c = dic.most_common()
mod = 10**9 + 7
 
ans = 0
d = []
for x in c:
    if x[1] == 1:
        break
    else:
        d.append(x[1])
b = len(c)-len(d)
 
ls =list(product([0, 1], repeat=len(d)))
 
for x in ls:
    num = 1
    for z, y in zip(d, x):
        if y == 1:
            num = (z*num)%mod
    ans = (ans + num*(2**b))%mod
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
#import sys
#sys.setrecursionlimit(10**7)

ans = []
temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    #print(S, N)

    #% (10^9 + 7)
    #return count_0
    N_l = len(N)
    #print(range(N_l)[::-1])
    #print(range(N_l))
    #global ans 
    #ans.append(N[-1])
    global temp_ans
    global ans
    for i in range(N_l)[::-1]:
    #for i in range(N_l):
        ans += N[i]
        if i == -N_l+1:
            temp_ans = ans
        b(N, N[i], i)
        #global temp_ans
        global temp_array
        ans += temp_array
        temp_ans = ans
        #print(temp_ans)
        #print(temp_array)
        temp_array = []
        #print(ans)
    #print(list(set(ans)))
    #print(10**9 + 7)

    # 2
    #return len(list(set(ans))) % (10**9 + 7)
    #print(ans)
    return len(ans) % (10**9 + 7)


def b(org_array, temp, i):
    global temp_array
    global temp_ans
    """
    if len(org_array) == 0:
        #global temp_array
        #global ans
        #ans += temp_array
        #print(temp_array)
        #temp_array = []
        return
    else:
        #print("org array:"+org_array)
        #ans.append(temp)
        #print(temp)
        #print(temp_array)
    """
    if len(temp_ans) != 0:
        temp_array += [temp+s for s in temp_ans if s[0] != temp and temp not in s]

        #for tmp in temp_array:
        #    b(org_array, tmp, i+1)
        """
        if temp not in temp_array:
            temp_array.append(temp)
        new_temp = temp
        b(org_array, new_temp, i+1)
        if org_array[i+1] not in temp:
            temp += org_array[i+1]
            if temp not in temp_array:
                temp_array.append(temp)
            b(org_array, temp, i+1)
        #print("org: "+org_array[i+1])
        #print(temp)
        #b(org_array, temp, i+1)
        """


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

array = []
for s in set(S):
    array.append(S.count(s))

len_array = len(array)

def recall(cur,t=1,total=0):
    if cur==len_array:
        return t
    for i in [array[cur],1]:
        total += recall(cur+1,t*i)
    return total

print((recall(0)-1)%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
sys.setrecursionlimit(10**7)

ans = []
temp_array = []
temp_ans = []


def problem_a(input_string):
    S = int(input_string[0].rstrip())
    N = input_string[1].rstrip()
    #print(S, N)

    #% (10^9 + 7)
    #return count_0
    N_l = len(N)
    #print(range(N_l)[::-1])
    #print(range(N_l))
    #global ans 
    #ans.append(N[-1])
    global temp_ans
    global ans
    for i in range(N_l)[::-1]:
    #for i in range(N_l):
        ans += N[i]
        if i == -N_l+1:
            temp_ans = ans
        b(N, N[i], i)
        #global temp_ans
        global temp_array
        ans += temp_array
        temp_ans = ans
        #print(temp_ans)
        #print(temp_array)
        temp_array = []
        #print(ans)
    #print(list(set(ans)))
    #print(10**9 + 7)

    # 2
    #return len(list(set(ans))) % (10**9 + 7)
    #print(ans)
    return len(ans) % (10**9 + 7)


def b(org_array, temp, i):
    global temp_array
    global temp_ans
    """
    if len(org_array) == 0:
        #global temp_array
        #global ans
        #ans += temp_array
        #print(temp_array)
        #temp_array = []
        return
    else:
        #print("org array:"+org_array)
        #ans.append(temp)
        #print(temp)
        #print(temp_array)
    """
    if len(temp_ans) != 0:
        temp_array += [temp+s for s in temp_ans if s[0] != temp and temp not in s]

        #for tmp in temp_array:
        #    b(org_array, tmp, i+1)
        """
        if temp not in temp_array:
            temp_array.append(temp)
        new_temp = temp
        b(org_array, new_temp, i+1)
        if org_array[i+1] not in temp:
            temp += org_array[i+1]
            if temp not in temp_array:
                temp_array.append(temp)
            b(org_array, temp, i+1)
        #print("org: "+org_array[i+1])
        #print(temp)
        #b(org_array, temp, i+1)
        """


def main():
    input_line = stdin.readlines()
    answer = problem_a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())	
C=0
def re(s,i):
  global C 
  if s!=[]:
    C+=1  
  for j in range(1,N-i):
    if not S[i+j] in s:
      re(s+[S[i+j]],i+j)
re([],-1)      
print(C%(10 ** 9 +7))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
s = input()
cnt = 0
for i in range(n)[::-1]:
  ch = s[i:]
  c = collections.Counter(ch)
  tmp_cnt = 1
  for k,v in c.items():
    if k != s[i]:
      tmp_cnt *= (v+1)
  cnt += tmp_cnt
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def is_unq(seq):
    return len(seq) == len(set(seq))

from itertools import combinations as cmb
n = int(input())
S = input()
Sli = list(S)
ans_s = [[] for _ in range(1,n+1)]
ans = 0
for x in range(n):
    lis = list(cmb(Sli,x+1))
    ans_s[x] += lis
    for y in range(len(ans_s[x])):
        flag = 0
        if is_unq(ans_s[x][y]) == True:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
import functools
import operator
prod = functools.partial(functools.reduce, operator.mul)

N = int(input())
S = list(input())  # len:N

S = sorted(S)  #  アルファベット順に並べ替え
U = [0] * 26  #  ユニーク数数え上げ
tmp = "0"
count = 0
u_count = 0
for i in range(N):
    if i > 0:
        if tmp == S[i]:
            U[count] += 1
        else:
            tmp = S[i]
            count += 1
            U[count] += 1
    else:
        tmp = S[i]
        U[count] += 1


for u in range(26):
    if U[u] != 0:
        u_count += 1

U = sorted(U)

for i in range(len(U)):
    if U[0] == 0:
        U.pop(0)

count = 0

for i in range(1, u_count+1):
    count += np.sum(np.array(list(map(prod, list(itertools.combinations(U, i))))))

a = 10 ** 9 + 7
print(count % a)