Problem:


Score : 600 points

Problem Statement

You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n.
It is known that each of the n integers 1,...,n appears at least once in this sequence.

For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7.

Notes

If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.

A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

Constraints

1 \leq n \leq 10^5

1 \leq a_i \leq n

Each of the integers 1,...,n appears in the sequence.

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_{n+1}

Output

Print n+1 lines.
The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7.

Sample Input 1

3
1 2 1 3

Sample Output 1

3
5
4
1

There are three subsequences with length 1: 1 and 2 and 3.

There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.

There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.

There is one subsequence with length 4: 1,2,1,3.

Sample Input 2

1
1 1

Sample Output 2

1
1

There is one subsequence with length 1: 1.

There is one subsequence with length 2: 1,1.

Sample Input 3

32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9

Sample Output 3

32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1

Be sure to print the numbers modulo 10^9+7.



Sample input:
3
1 2 1 3


Sample output:
3
5
4
1

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().strip().split()))

def d_print(subdict):
    length = len(subdict) % int(1e9+7)
    print(length)

def d_search(previous_dict):
    d = dict()
    for item in previous_dict.keys():
        temp = previous_dict[item]
        left = set(a[temp+1:])
        for i in left:
            key = list(item) + [i]
            v = temp + a[temp+1:].index(i) + 1
            k = tuple(k)
            d[k] = v
    return d

s = set(a)
d = dict()
for i in s:
    d[(i,)] = a.index(i)
d_print(d)

for k in range(n):
    d = d_search(d)
    d_print(d)

Submission status: Runtime Error

Solution code:
import functools
import operator


def __nCr(n, r):
    r = min(r, n - r)
    if r == 0:
        return 1
    numer = functools.reduce(operator.mul, range(n, n - r, -1))
    denom = functools.reduce(operator.mul, range(1, r + 1))
    return numer // denom

MOD = 1000000007
n = int(input())
a = list(map(int, input().split()))
pos = [-1 for _ in range(n + 1)]
for i in range(n + 1):
    if pos[a[i]] > -1:
        l, r = pos[a[i]], i
    else:
        pos[a[i]] = i

# print(l, r)

ans = []
ans.append(str(n))
for k in range(2, n + 1):
    ttl = __nCr(n + 1, k)
    if l >= k - 1:
        subl = __nCr(l, k - 1)
        # print(k, 'subl', subl)
        ttl -= subl
    if (n - r) >= k - 1:
        subr = __nCr(n - r, k - 1)
        # print(k, 'subr', subr)
        ttl -= subr
    if l + (n - r) >= k - 1 and k >= 3:
        subx = 0
        for b in range(1, l + 1):
            # print('b', b, 'l', l, 'n - r', n - r, 'k - b', k - b)
            if n - r >= k - b - 1:
                subx += __nCr(l, b) * __nCr(n - r, (k - b - 1))
        # subx = l * (n - r) * __nCr(l + (n - r) - 2, k - 3)
        # print(k, 'subx', subx)
        ttl -= subx
    ans.append(str(ttl % MOD))
ans.append(str(1))
print('\n'.join(ans))

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

def com(n, k):
    if n == 0 or k == 0:
        return 1
    return int(reduce(mul, range(n + 1 - k, n + 1)) / reduce(mul, range(1, k + 1)))

idx_list = [-1] * (n + 2)
l = 0
r = 0
for i, a_i in enumerate(a):
    if idx_list[a_i] < 0:
        idx_list[a_i] = i
    else:
        l = idx_list[a_i]
        r = n - i
        break

for k in range(1, n + 2):
    m = k - 1
    dup = 0
    for l_n in range(0, m + 1):
        r_n = m - l_n
        if l_n > l or r_n > r:
            continue
        l_c = com(l, l_n)
        r_c = com(r, r_n)
        dup += l_c * r_c
    ans = (com(n + 1, k) - dup) % (10 ** 9 + 7)
    print(ans)

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

def com(n, k):
    if n == 0 or k == 0:
        return 1
    return int(reduce(mul, range(n + 1 - k, n + 1)) / reduce(mul, range(1, k + 1)))

idx_list = [-1] * (n + 1)
l = 0
r = 0
for i, a_i in enumerate(a):
    if idx_list[a_i] < 0:
        idx_list[a_i] = i
    else:
        l = idx_list[a_i]
        r = n - i
        break

for k in range(1, n + 2):
    m = k - 1
    dup = 0
    for l_n in range(0, m + 1):
        r_n = m - l_n
        if l_n > l or r_n > r:
            continue
        l_c = com(l, l_n)
        r_c = com(r, r_n)
        dup += l_c * r_c
    ans = (com(n + 1, k) - dup) % (10 ** 9 + 7)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

def comb(n, k):
    if n<k:
        return 0
    if k<0:
        return 0

    if n-k<k:
        k=n-k
    return round(math.factorial(n)/math.factorial(k)/math.factorial(n-k))

n=int(input())
a=np.array([int(i) for i in input().split()])

count_lst=[[] for _ in range(n)]
for i in range(n+1):
    count_lst[a[i]-1].append(i)

for i in range(n):
    if len(count_lst[i])==2:
        left=count_lst[i][0]
        right=count_lst[i][1]
gapLen=right-left-1

del count_lst


base=10**9+7
for k in range(1, n+2):
    #print("k={}".format(k))
    retVal=0
    retVal+=comb(n-1, k-2)#両方in
    #print(retVal)
    retVal+=comb(n-1, k)#両方out
    #print(retVal)
    
    retVal+=comb(n-1-gapLen, k-1)#片方in, gapから1つも選ばれない
    #print(retVal)    
    #retVal+=2*(comb(n-1, k-1)-comb(n-1-gapLen, k-1))
    print(retVal%base)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

def comb(n, k):
    if n<k:
        return 0
    if k<0:
        return 0

    if n-k<k:
        k=n-k
    return round(math.factorial(n)/math.factorial(k)/math.factorial(n-k))

n=int(input())
a=np.array([int(i) for i in input().split()])

count_lst=[[] for _ in range(n)]
for i in range(n+1):
    count_lst[a[i]-1].append(i)

for i in range(n):
    if len(count_lst[i])==2:
        left=count_lst[i][0]
        right=count_lst[i][1]
gapLen=right-left-1

del count_lst


base=10**9+7
for k in range(1, n+2):
    #print("k={}".format(k))
    retVal=0
    retVal+=comb(n-1, k-2)#両方in
    #print(retVal)
    retVal+=comb(n-1, k)#両方out
    #print(retVal)
    
    retVal+=comb(n-1-gapLen, k-1)#片方in, gapから1つも選ばれない
    #print(retVal)    
    retVal+=2*(comb(n-1, k-1)-comb(n-1-gapLen, k-1))
    print(retVal%base)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python

import math

R = 1000000007
def C(m, n):
    if m < 0 or n < 0 or m < n: return 0
    if m == 0 or n == 0: return 1
    return int(math.factorial(m)/math.factorial(n)/math.factorial(m-n))
    ans = 1
    u, l = m, n
    while u > m - n:
        ans *= u
        if l > 1 and ans % l == 0:
            ans /= l
            l -= 1
        u -= 1
    if l > 1:
        i = l
        while i > 1:
            ans /= i
            i -= 1
    return ans

n = int(input())
a = [int(i) - 1 for i in input().split(' ')]

left = right = 0
index = [-1 for i in range(n + 1)]
for i, v in enumerate(a):
    if index[v] >= 0:
        left = index[v]
        right = n - i
        break
    index[v] = i

print(n)
k = 2
while k <= n:
    all = C(n + 1, k) % R
    for i in range(k):
        all -= (C(left, i) * C(right, k - i - 1)) % R
    print(all)
    k += 1
print(1)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

def comb(n, k):
    if n<k:
        return 0
    if k<0:
        return 0

    if n-k<k:
        k=n-k
    return round(math.factorial(n)/math.factorial(k)/math.factorial(n-k))

n=int(input())
a=np.array([int(i) for i in input().split()])

count_lst=[[] for _ in range(n)]
for i in range(n+1):
    count_lst[a[i]-1].append(i)

for i in range(n):
    if len(count_lst[i])==2:
        left=count_lst[i][0]
        right=count_lst[i][1]
gapLen=right-left-1


base=10**9+7
for k in range(1, n+2):
    #print("k={}".format(k))
    retVal=0
    retVal+=comb(n-1, k-2)#両方in
    #print(retVal)
    retVal+=comb(n-1, k)#両方out
    #print(retVal)
    
    retVal+=comb(n-1-gapLen, k-1)#片方in, gapから1つも選ばれない
    #print(retVal)    
    retVal+=2*(comb(n-1, k-1)-comb(n-1-gapLen, k-1))
    print(retVal%base)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python

import math

def C(m, n):
    if m < 0 or n < 0 or m < n: return 0
    if m == 0 or n == 0: return 1
    return int(math.factorial(m)/math.factorial(n)/math.factorial(m-n))
    ans = 1
    u, l = m, n
    while u > m - n:
        ans *= u
        if l > 1 and ans % l == 0:
            ans /= l
            l -= 1
        u -= 1
    if l > 1:
        i = l
        while i > 1:
            ans /= i
            i -= 1
    return ans

n = int(input())
a = [int(i) - 1 for i in input().split(' ')]

left = right = 0
index = [-1 for i in range(n + 1)]
for i, v in enumerate(a):
    if index[v] >= 0:
        left = index[v]
        right = n - i
        break
    index[v] = i

print(n)
k = 2
while k <= n:
    all = C(n + 1, k)
    for i in range(k):
        all -= C(left, i) * C(right, k - i - 1)
    print(all % 1000000007)
    k += 1
print(1)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

def comb(n, k):
    if n<k:
        return 0
    if k<0:
        return 0

    if n-k<k:
        k=n-k
    return round(math.factorial(n)/math.factorial(k)/math.factorial(n-k))

n=int(input())
a=np.array([int(i) for i in input().split()])

count_lst=[[] for _ in range(n)]
for i in range(n+1):
    count_lst[a[i]-1].append(i)

for i in range(n):
    if len(count_lst[i])==2:
        left=count_lst[i][0]
        right=count_lst[i][1]
gapLen=right-left-1


base=10**9+7
for k in range(1, n+2):
    #print("k={}".format(k))
    retVal=0
    retVal+=comb(n-1, k-2)#両方in
    #print(retVal)
    retVal+=comb(n-1, k)#両方out
    #print(retVal)
    
    retVal+=comb(n-1-gapLen, k-1)#片方in, gapから1つも選ばれない
    #print(retVal)
    for i in range(1, gapLen+1):
        retVal+=2*comb(gapLen, i)*comb(n-1-gapLen, k-1-i)
    print(retVal%base)

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(map(int,input().split()))
pos = [0 for _ in range(n+1)]

first = 0
second = 0
for i in range(n+1):
    if(pos[a[i]]):
        first = pos[a[i]]-1
        second = i
    pos[a[i]] = i+1

cnt = n+1
cnt2 = 1

MOD = 1000000007

for i in range(n+1):
    print(round(cnt-cnt2)%MOD)

    cnt = cnt*(n-i)/(i+2)
    cnt2 = cnt2*(n-second-i+first)/(i+1)

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
c=[]
for i in range(N+1):
  if a[i] in c:
    b=a[i]
    j=c.index(a[i])
    k=i
    L=j+N-k
    break
  c+=[a[i]]
Base=N+1
Minu=1
for i in range(0,min(L+1,N+1)):
  print((Base-Minu)%(10**9+7))
  Base=int(Base*(N-i)/(i+2))
  Minu=int(Minu*(L-i)/(i+1))

for i in range(L+1,N+1):
  print(Base%(10**9+7))
  Base=int(Base*(N-i)/(i+2))

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(map(int,input().split()))
pos = [0 for _ in range(n+1)]

first = 0
second = 0
for i in range(n+1):
    if(pos[a[i]]):
        first = pos[a[i]]-1
        second = i
    pos[a[i]] = i+1

cnt = n+1
cnt2 = 1

MOD = 1000000007

for i in range(n+1):
    print((cnt-cnt2)%MOD)

    cnt *= (n-i)/(i+2)
    cnt = int(cnt)
    cnt2 *= (n-second-i+first)/(i+1)
    cnt2 = int(cnt2)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
a=list(map(int,input().split()))
c=[]
for i in range(N+1):
  if a[i] in c:
    b=a[i]
    j=c.index(a[i])
    k=i
    L=j+N-k
    break
  c+=[a[i]]
Base=N+1
Minu=1
for i in range(0,min(L+1,N+1)):
  print((Base-Minu)%(10**9+7))
  Base=int(Base*(N-i)/(i+2))
  Minu=int(Minu*(L-i)/(i+1))
  #print(int(math.factorial(N+1)/(math.factorial(i+1)*math.factorial(N-i))-math.factorial(L)/(math.factorial(i)*math.factorial(L-i))))
for i in range(L+1,N+1):
  print(Base%(10**9+7))
  Base=int(Base*(N-i)/(i+2))

Submission status: Runtime Error

Solution code:
import math
N=int(input())
a=list(map(int,input().split()))
c=[]
for i in range(N+1):
  if a[i] in c:
    b=a[i]
    j=c.index(a[i])
    k=i
    L=j+N-k
    break
  c+=[a[i]]
base=N+1
minu=1
for i in range(0,min(L+1,N+1)):
  print((base-minu)%(10**9+7))
  base=int(base*(N-i)/(i+2))
  minu=int(minu*(L-i)/(i+1))
  #print(int(math.factorial(N+1)/(math.factorial(i+1)*math.factorial(N-i))-math.factorial(L)/(math.factorial(i)*math.factorial(L-i))))
for i in range(L+1,N+1):
  print(base%(10**9+7))
  base=int(base*(N-i)/(i+2))