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: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def com(com_n, com_r):
    if com_n<com_r:return 0
    return fac[com_n] * inv[com_r] * inv[com_n - com_r] % md

# combinationの準備
# md>n_maxの条件
md = 10**9+7
n_max = 10**5+3
fac = [1]
inv = [1] * (n_max + 1)
k_fac_inv = 1
for i in range(1, n_max + 1):
    k_fac_inv = k_fac_inv * i % md
    fac.append(k_fac_inv)
k_fac_inv = pow(k_fac_inv, md - 2, md)
for i in range(n_max, 1, -1):
    inv[i] = k_fac_inv
    k_fac_inv = k_fac_inv * i % md

def main():
    n=int(input())
    aa=LI()
    pos=[-1]*(n+3)
    for i,a in enumerate(aa):
        if pos[a]==-1:pos[a]=i
        else:lr=n-i+pos[a]
    for k in range(1,n+2):
        ans=com(n+1,k)-com(lr,k-1)
        print(ans%md)

main()

Submission status: Accepted

Solution code:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**5+5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



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

x = y = 0
b = [-1 for i in range(n)]
for i in range(len(a)):
    if b[a[i]] != -1:
        x = b[a[i]]
        y = i
        break 
    else:
        b[a[i]] = i

for k in range(1,n+1):
    print((cmb(n,k,mod)-cmb(n+x-y-1,k-1,mod))%mod)

Submission status: Accepted

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

from collections import Counter
c = Counter(a)
for k in c.keys():
    if c[k] == 2:
        break
l = []
for i in range(len(a)):
    if a[i] == k:
        l.append(i)
    if len(l) == 2:
        break
d = n - l[1] + l[0]

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y // 2) ** 2 % mod
    else            : return power(x, y // 2) ** 2 * x % mod

mod = 10 ** 9 + 7
N = n + 2

factorial = [1]
for i in range(1, N):
    factorial.append(factorial[i - 1] * i % mod)

inverseFactorial = [0] * N
inverseFactorial[N - 1] = power(factorial[N - 1], mod - 2)
for i in range(N - 2, -1, -1):
    inverseFactorial[i] = inverseFactorial[i + 1] * (i + 1) % mod

def comb(x, y):
    return factorial[x] * inverseFactorial[y] * inverseFactorial[x - y] % mod

for k in range(1, n + 2):
    if d >= k - 1:
        print((comb(n+1, k) - comb(d, k-1)) % mod)
    else:
        print(comb(n+1, k))

Submission status: Accepted

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

from collections import Counter
c = Counter(a)
for k in c.keys():
    if c[k] == 2:
        break
l = []
for i in range(len(a)):
    if a[i] == k:
        l.append(i)
    if len(l) == 2:
        break
d = n - l[1] + l[0]

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y // 2) ** 2 % mod
    else            : return power(x, y // 2) ** 2 * x % mod

mod = (10 ** 9 + 7)

N1 = n + 2
factorial1 = [1]
for i in range(1, N1):
    factorial1.append(factorial1[i - 1] * i % mod)
inverseFactorial1 = [0] * N1
inverseFactorial1[N1 - 1] = power(factorial1[N1 - 1], mod - 2)
for i in range(N1 - 2, -1, -1):
    inverseFactorial1[i] = inverseFactorial1[i + 1] * (i + 1) % mod
def combi1(x, y):
    return factorial1[x] * inverseFactorial1[y] * inverseFactorial1[x - y] % mod

for k in range(1, n + 2):
    if d >= k - 1:
        print((combi1(n+1, k) - combi1(d, k-1)) % mod)
    else:
        print(combi1(n+1, k))

Submission status: Accepted

Solution code:
def main():
    from collections import Counter as ct
    fact=[1]
    mod=10**9+7
    n=int(input())
    a=list(map(int,input().split()))
    for i in range(1,n+2):
        fact.append((fact[-1]*i)%mod)
    def inv_n(n,mod=10**9+7):return pow(n,mod-2,mod)
    def nCr(n,r,mod=10**9+7):return inv_n(fact[n-r]*fact[r]%mod,mod)*fact[n]%mod
    c=ct(a).most_common()[0][0]
    p,q=-1,-1
    for i in range(n+1):
        if a[i]==c:
            if p==-1:
                p=i
            else:
                q=i
    x=p+n-q
    for k in range(1,n+2):
        ans=0
        if k-2>=0:
            ans+=nCr(n-1,k-2)
        if k<=n-1:
            ans+=nCr(n-1,k)
        if k<=n:
            ans+=2*nCr(n-1,k-1)
        if k-1<=x:
            ans-=nCr(x,k-1)
        print((ans+mod)%mod)
main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
class modfact(object):
    def __init__(self,n):
        fact=[1]*(n+1)
        invfact=[1]*(n+1)
        for i in range(1,n+1):
            fact[i]=i*fact[i-1]%MOD
        invfact[n]=pow(fact[n],MOD-2,MOD)
        for i in range(n-1,-1,-1):
            invfact[i]=invfact[i+1]*(i+1)%MOD
        self.__fact=fact
        self.__invfact=invfact

    def fact(self,n):
        return self.__fact[n]

    def invfact(self,n):
        return self.__invfact[n]

    def comb(self,n,k):
        if(k<0 or n-k<0): return 0
        return (self.fact(n)*self.invfact(k)*self.invfact(n-k))%MOD

from collections import Counter
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    C=Counter(A).most_common()
    a=C[0][0]
    for i in range(n+1):
        if(A[i]==a):
            A[i]=0
            l=i
            break
    for i in range(n+1):
        if(A[i]==a):
            A[i]=0
            r=i

    mf=modfact(n+1)
    for k in range(1,n+2):
        print((mf.comb(n+1,k)-mf.comb(l+n-r,k-1))%MOD)
resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(a) for a in input().split()]

pos = [-1]*n
for i in range(n+1):
    if pos[A[i]-1] >= 0:
        idx1 = pos[A[i]-1]
        idx2 = i
        break
    pos[A[i]-1] = i

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, n + 2 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


ans = [0]*(n+2)
for k in range(1, n+2):
    ans[k] = cmb(n+1, k, mod)
    

print(n)
for k in range(2, n+2):
    c = cmb(idx1+n-idx2, k-1, mod)
    print((ans[k]-c)%mod)

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


class Combinations:
    def __init__(self, max_num, mod):
        self.mod = mod
        self.factorials = [1]
        for i in range(1, max_num + 1):
            self.factorials.append((self.factorials[-1] * i) % mod)
        self.invs = [pow(self.factorials[-1], mod - 2, mod)]
        for i in reversed(range(1, max_num + 1)):
            self.invs.append(self.invs[-1] * i % mod)
        self.invs = self.invs[::-1]

    def __call__(self, n, k):
        if n < 0 or k < 0 or k > n:
            return 0
        return self.factorials[n] * self.invs[k] * self.invs[n - k] % self.mod


MOD = 10**9 + 7

n = ri()
ls = ril()
counter = collections.Counter(ls)
d = counter.most_common()[0][0]
l, r = [i for i in range(n + 1) if ls[i] == d]

combinations = Combinations(n + 1, MOD)
for k in range(1, n + 2):
    res = combinations(n + 1, k) - combinations(l + n - r, k - 1)
    res += MOD
    res %= MOD
    print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I() + 1
A = LI()


fac = [1] * (n + 1)
inv = [1] * (n + 1)
for j in range(1, n + 1):
    fac[j] = fac[j-1] * j % mod


inv[n] = pow(fac[n], mod-2, mod)
for j in range(n-1, -1, -1):
    inv[j] = inv[j+1] * (j+1) % mod


def comb(n, r):
    if r > n or n < 0 or r < 0:
        return 0
    return fac[n] * inv[n - r] * inv[r] % mod


D = defaultdict(lambda:-1)
for i in range(n + 1):
    if D[A[i]] != -1:
        l = D[A[i]]
        r = n - i - 1
        break
    else:
        D[A[i]] = i


for k in range(1, n + 1):
    print((comb(n, k) - comb(l + r, k - 1)) % mod)

Submission status: Accepted

Solution code:
class combination():
    #素数のmod取るときのみ　速い
    def __init__(self, n, mod):
        self.n = n
        self.fac = [1] * (n + 1)
        self.inv = [1] * (n + 1)
        for j in range(1, n + 1):
            self.fac[j] = self.fac[j - 1] * j % mod

        self.inv[n] = pow(self.fac[n], mod - 2, mod)
        for j in range(n - 1, -1, -1):
            self.inv[j] = self.inv[j + 1] * (j + 1) % mod

    def comb(self, n, r, mod):
        if r > n or n < 0 or r < 0:
            return 0
        return self.fac[n] * self.inv[n - r] * self.inv[r] % mod

def examD(mod):
    N = I()
    a = LI()
    c = Counter(a)
    db = c.most_common()[0][0]
    curL = a.index(db)
    curR = a.index(db, curL + 1)
    L = curL; M = curR-curL-1;  R = N - curR
#    print(curL,curR)
#    print(L,M,R)
    C = combination(N+1,mod)
    ans = []
    ans.append(N)
    for i in range(2,N+2):
        cur = C.comb(N+1,i,mod)
        if N-1-M-(i-1)>=0:
            cur -= C.comb(N-1-M,i-1,mod)
        if cur<0:
            cur += mod
        ans.append(cur)
    for v in ans:
        print(v)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD(mod)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

class Combination(object):
	def __init__(self, N, con):
		self.fac = [0] * (N + 1)
		self.inv = [0] * (N + 1)
		self.finv = [0] * (N + 1)
		self.fac[0], self.fac[1] = 1, 1
		self.inv[1] = 1
		self.finv[0], self.finv[1] = 1, 1

		# 前計算
		for i in range(2, N + 1):
			self.fac[i] = self.fac[i - 1] * i % con
			self.inv[i] = self.inv[con % i] * (con - (con // i)) % con
			self.finv[i] = self.finv[i - 1] * self.inv[i] % con

	def com(self, N, k):
		return (self.fac[N] * self.finv[k] * self.finv[N - k]) % con

#処理内容
def main():
	N = int(input())
	A = getlist()
	x = 0
	y = 0
	D = defaultdict(lambda: -1)
	for i in range(N + 1):
		if D[A[i]] == -1:
			D[A[i]] = i
		else:
			x = D[A[i]]
			y = i
			break
	n = y - x
	if N - n == 0:
		C1 = Combination(N + 1, con)
		print(C1.com(N + 1, i) - 1)
		for i in range(2, N + 2):
			print(C1.com(N + 1, i))
	else:
		C1 = Combination(N + 1, con)
		C2 = Combination(N - n, con)
		for i in range(1, N - n + 2):
			print((C1.com(N + 1, i) - C2.com(N - n, i - 1)) % con)
		for i in range(N - n + 2, N + 2):
			print(C1.com(N + 1, i))

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
show_flg=True

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(2,n+1):
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        if self.fac[n]!=0:
            return self.fac[n]
        self.fac[n]=n*self.fact(n-1)%mo
        return self.fac[n]

    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
    
n=int(input())
a=[int(i) for i in input().split()]

cm=Comb(n+1)
c=Counter(a)
db=c.most_common()[0][0]
fst=a.index(db)
lst=a.index(db,fst+1)
x=n+1-(lst-fst+1)

for i in range(1,n+2):
    ans=cm.comb(n+1,i)-cm.comb(x,i-1)
    print(ans%mo)

Submission status: Accepted

Solution code:
mod=10**9+7
MAX=10**5+1000

g1=[1,1]
g2=[1,1]
for i in range(2,MAX+1):
    num_1=g1[-1]*i%mod
    g1.append(num_1)
    g2.append(pow(num_1,mod-2,mod))
    
def cmb(n,r,MOD):
    return g1[n]*g2[r]*g2[n-r]%MOD

n=int(input())
a=list(map(int,input().split()))
data=[0]*(n+1)
for i in range(n+1):
    data[a[i]]+=1
for i in range(1,n+1):
    if data[i]==2:
        b=i
        break
l=[]
for i in range(n+1):
    if a[i]==b:
        l.append(i)
c=l[1]-l[0]+1
for k in range(1,n+3-c):
    ans=cmb(n+1,k,mod)-cmb(n+1-c,k-1,mod)
    print(ans%mod)
for k in range(n+3-c,n+2):
    ans=cmb(n+1,k,mod)
    print(ans)

Submission status: Accepted

Solution code:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**5+1
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
    
n=int(input())
a=list(map(int,input().split()))
data=[0]*(n+1)
for i in range(n+1):
    data[a[i]]+=1
for i in range(1,n+1):
    if data[i]==2:
        b=i
        break
l=[]
for i in range(n+1):
    if a[i]==b:
        l.append(i)
c=l[1]-l[0]+1
for k in range(1,n+3-c):
    ans=cmb(n+1,k,mod)-cmb(n+1-c,k-1,mod)
    print(ans%mod)
for k in range(n+3-c,n+2):
    ans=cmb(n+1,k,mod)
    print(ans)

Submission status: Accepted

Solution code:
class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0

from collections import *
MOD = 10 ** 9 + 7
# n = int(input())
# a = map(int, input().split())
n, *a = map(int, open(0).read().split())
v, _ = Counter(a).most_common(1)[0]
l = -1
for i, b in enumerate(a):
    if b == v:
        if l < 0:l = i
        else: r = n - i
c = Factorial(n + 1, MOD).comb
for k in range(n + 1):
    print ((c(n + 1, k + 1) - c(l + r, k)) % MOD)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
p=[-1]*N
x=0
for i in range(N):
    if p[A[i]-1]==-1:
        p[A[i]-1]=i
    else:
        x=p[A[i]-1]+N-i
        break
m=10**9+7
print(N)
a,b=N+1,1
def sub(a,b):
    return (a-b)%m
def mul(a,b):
    return ((a%m)*(b%m))%m
def div(a,b):
    return mul(a,pow(b,m-2,m))
for k in range(2,N+2):
    a=div(a*(N+2-k),k)
    b=div(b*(x+2-k),k-1)
    print(sub(a,b))

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
n = int(input())
a = list(map(int, input().split()))
lst = [-1] * n

# O(N)
for i in range(n + 1):
    if lst[a[i] - 1] != -1:
        first = lst[a[i] - 1]
        second = i
        break
    lst[a[i] - 1] = i

# print (first, second)    
left = first
right = n - second

U = 10 ** 5 + 1
def power_mod(a, n):
    if n == 0:
        return 1
    x = power_mod(a//2)

def make_fact(fact, fact_inv):
    for i in range(1, U + 1):
        fact[i] = (fact[i - 1] * i) % MOD
    fact_inv[U] = pow(fact[U], MOD - 2, MOD)
    for i in range(U, 0, -1):
        fact_inv[i - 1] = (fact_inv[i] * i) % MOD

def comb(n, k):
    if k < 0 or n < k:
        return 0
    x = fact[n]
    x *= fact_inv[k]
    x %= MOD
    x *= fact_inv[n - k]
    x %= MOD
    return x

fact = [1] * (U + 1)
fact_inv = [1] * (U + 1)
make_fact(fact, fact_inv)

for i in range(1, n + 2):
    ans = comb(n + 1, i) - comb(left + right, i - 1)
    print (ans % MOD)

Submission status: Accepted

Solution code:
import collections
mod = 10**9+7
fac = [1]
caf = [1]
n=int(input())

def pow(n,p):
    res=1
    while p >0:
        if p%2==0:
            n = n**2 % mod
            p //= 2
        else:
            res = res * n % mod
            p -= 1
    return res % mod

for i in range(1,n+2):
    fac.append((fac[i-1]*i)%mod)
    caf.append(pow(fac[i],mod-2)%mod)

def ncr(n,r):
    if n < r: return 0
    return fac[n]*caf[r]*caf[n-r]

def main():
    global n
    a=list(map(int,input().split()))

    ca= collections.Counter(a)
    tyo= ca.most_common()[0][0]
    kaburi=[i for i, x in enumerate(a) if x == tyo]

    for i in range(1,n+2):
        print((ncr(n+1,i)-ncr(n+1-(kaburi[1]-kaburi[0]+1),i-1))%mod)

main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from math import factorial
input = sys.stdin.buffer.readline
mod = 10**9+7

# comb init
nmax = 10 ** 5 + 2  # change here
fac = [0] * (nmax + 1)
finv = [0] * (nmax + 1)
inv = [0] * (nmax + 1)
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, nmax):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod


def comb(n, r):
    if n < r:
        return 0
    else:
        return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod


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

pos = defaultdict(lambda: -1)
for i in range(N+1):
    if pos[a[i]] == -1:
        pos[a[i]] = i
    else:
        same = (pos[a[i]], i)
        break

for k in range(1, N+2):
    print((comb(N+1, k) - comb(same[0] + (N-same[1]), k-1))%mod)

Submission status: Accepted

Solution code:
MOD=10**9+7
p=100009
fact=[0]*p 
fact[0]=1 
for i in range(1,p):
    fact[i]=(fact[i-1]*i)%MOD
def MI(a,MOD):
    return pow(a,MOD-2,MOD)
def nck(n, k,MOD):
    if k<0:
        return 0 
    if n==k or k==0:
        return 1 
    if n<k:
        return 0 
    return (fact[n]*MI(fact[k],MOD)%MOD*MI(fact[n-k],MOD)%MOD)%MOD

n=int(input())
l=[int(i) for i in input().split()]
from collections import Counter
c=Counter(l)
for i  in c:
    if c[i]>=2:
        ans=i 
        break 
ind1=-1 
ind2=-1 
for i in range(n+1):
    if l[i]==ans:
        if ind1==-1:
            ind1= i 
        else: 
            ind2=i 
MOD=10**9+7 
for i in range(1,n+2):
    a=nck(n+1,i,MOD)
    b=nck(n-(ind2-ind1),i-1,MOD)
    print((a-b+MOD)%MOD)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I() + 1
A = LI()
D = defaultdict(lambda:-1)
for i in range(n):
    if D[A[i]] != -1:
        l = D[A[i]]
        r = n - i - 1
        break
    else:
        D[A[i]] = i


fac = [1] * (n + 1)
inv = [1] * (n + 1)
for j in range(1, n + 1):
    fac[j] = fac[j-1] * j % mod


inv[n] = pow(fac[n], mod-2, mod)
for j in range(n-1, -1, -1):
    inv[j] = inv[j+1] * (j+1) % mod


def comb(n, r):
    if r > n or n < 0 or r < 0:
        return 0
    return fac[n] * inv[n - r] * inv[r] % mod


for j in range(1, n + 1):
    print((comb(n, j) - comb(r + l, j - 1)) % mod)

Submission status: Accepted

Solution code:
def find_power(n):
    # 0!からn!までのびっくりを出してくれる関数
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist

def find_inv_power(n):
    #0!からn!までの逆元を10**9+7で割ったあまりリストを作る関数
    powlist=find_power(n)
    check=powlist[-1]
    first=1
    uselist=[0]*(n+1)
    secondlist=[0]*30
    secondlist[0]=check
    secondlist[1]=check**2
    for i in range(28):
        secondlist[i+2]=(secondlist[i+1]**2)%(10**9+7)
    a=format(10**9+5,"b")
    for j in range(30):
        if a[29-j]=="1":
            first=(first*secondlist[j])%(10**9+7)
    uselist[n]=first
    for i in range(n,0,-1):
        uselist[i-1]=(uselist[i]*i)%(10**9+7)
    return uselist

n=int(input())
lists=list(map(int,input().split()))
import collections
c=collections.Counter(lists)
d=c.most_common()
number=d[0][0]
listss=[]
for i in range(n+1):
    if lists[i]==number:
        listss.append(i+1)
a,b=listss[0],listss[1]

powlist=find_power(n+1)
invlist=find_inv_power(n+1)
def combi(n,r):
    a=powlist
    b=invlist
    if n<r:
        return 0
    elif n>=r:
        return (a[n]*b[r]*b[n-r])%(10**9+7)
    
for i in range(1,n+2):
    if i==1:
        print(n)
    elif 2<=i and i<=n+1-(b-a):
        print((combi(n-1,i)+combi(n-1,i-2)+2*combi(n-1,i-1)-combi(n+a-b,i-1))%(10**9+7))
    else:
        print(combi(n+1,i)%(10**9+7))

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque

def comb(n, r, mod, bunsi):
    if r > n:
        return 0
    ans = bunsi[n] * pow(bunsi[r], mod - 2, mod) * pow(bunsi[n - r], mod - 2, mod)
    return ans


def main():
    num = int(input())
    data = list(map(int, input().split()))

    mod = 10 ** 9 + 7

    count_data = Counter(data)
    count_data = sorted(count_data.items(), key=lambda x: -x[1])
    two_num = count_data[0][0]

    flg = 0
    left, mid, right = 0, 0, 0

    for i in range(num + 1):
        if data[i] == two_num:
            flg += 1
        elif flg == 0:
            left += 1
        elif flg == 1:
            mid += 1
        else:
            right += 1

    bunsi = [0 for i in range(num + 10)]
    bunsi[1] = 1
    bunsi[0] = 1
    for i in range(2, num + 10):
        bunsi[i] = (bunsi[i - 1] * i) % mod


    for now_num in range(1, num + 2):
        ans = 0
        if now_num == 1:
            ans += num
        elif now_num == 2:
            ans += comb(num - 1, 2, mod, bunsi)
            ans += 1
            ans += left + right + mid * 2
        else:
            ans += comb(num - 1, now_num, mod, bunsi)
            ans += comb(num - 1, now_num - 2, mod, bunsi)
            ans += comb(num - 1, now_num - 1, mod, bunsi) * 2 - comb(left, now_num - 1, mod, bunsi) - comb(right, now_num - 1, mod, bunsi)
            kari = comb(left + right, now_num - 1, mod, bunsi) - comb(left, now_num - 1, mod, bunsi) - comb(right, now_num - 1, mod, bunsi)
            ans -= kari

        ans %= mod
        print(ans)


if __name__ == '__main__':
    main()
    # test()

Submission status: Accepted

Solution code:
def d_11(MOD=10**9 + 7):
    class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ["mod", "factorial", "inverse"]

        def __init__(self, max_n: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_n + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_n, 0, -1):
                inv_append(inv[-1] * i % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

    N = int(input())
    A = [int(i) for i in input().split()]
    # 重複した数のインデックスを求める
    appeared = set()
    for k, a in enumerate(A):
        if a in appeared:
            left, right = A.index(a), k
            break
        appeared.add(a)

    c = Combination(N + 1).combination
    ans = [(c(N + 1, k) - c(N - (right - left), k - 1)) % MOD for k in range(1, N + 2)]
    return '\n'.join(map(str, ans))

print(d_11())

Submission status: Accepted

Solution code:
def d_11(MOD=10**9 + 7):
    from collections import Counter

    class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ["mod", "factorial", "inverse"]

        def __init__(self, max_n: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_n + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_n, 0, -1):
                inv_append(inv[-1] * i % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

    N = int(input())
    A = [int(i) for i in input().split()]
    # 重複した数のインデックスを求める
    c = Counter(A).most_common(n=1)[0][0]  # 重複した整数の値を取得
    index = [i for i, x in enumerate(A) if x == c]  # 重複した整数のインデックス
    left = index[0] + 1
    right = index[1] + 1

    comb = Combination(N + 1).combination
    ans = [(comb(N + 1, k) - comb(N - (right - left), k - 1)) % MOD for k in range(1, N + 2)]
    return '\n'.join(map(str, ans))

print(d_11())

Submission status: Accepted

Solution code:
from math import factorial as f
from collections import Counter as co
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
d=co(a)
keys=[k for k,v in d.items() if v==2]
dbl=keys[0]
ll=[i for i,x in enumerate(a) if x==dbl]
b=ll[1]-ll[0]
m=n-b

def pf(x,y,p):
  if y==0: return 1
  if y%2==0:
    d=pf(x,y//2,p)
    return d*d%p
  if y%2==1:
    return (x*pf(x,y-1,p))%p

facl=[1]
for i in range(1,n+2):
  facl.append(facl[i-1]*i%mod)
invl=[1]*(n+2)
for i in range(1,n+2):
  invl[i]=pf(facl[i],mod-2,mod)

def comb(x,k,p):
  if x<0 or k<0 or x<k: return 0
  if x==0 or k==0: return 1
  return (facl[x]*invl[k]*invl[x-k])%mod

for k in range(1,n+2):
  print((comb(n+1,k,mod)-comb(m,k-1,mod))%mod)

Submission status: Accepted

Solution code:
from collections import Counter

mod = 10**9 + 7

def init(n):
    # 階乗とその逆元をグローバルに定義
    global factorial, inverse
    # 0-indexedでそれぞれ１で初期化
    factorial = [1]*(n+1)
    inverse = [1]*(n+1)

    fact = 1
    for p in range(1,n+1):
        fact *= p
        fact %= mod
        factorial[p] = fact

    inv = pow(fact, mod-2, mod)
    inverse[n] = inv
    for p in range(2,n+1)[::-1]:
        inv *= p
        inv %= mod
        inverse[p-1] = inv

def comb(n,r):
    if n < r:
        return 0
    return factorial[n] * inverse[n-r] * inverse[r] % mod

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

b = Counter(a).most_common()[0][0]
pos = []
for i, ai in enumerate(a):
    if ai == b:
        pos.append(i)

l, r = pos
L, R = l, n-r

init(n+1)

for k in range(1, n+2):
    ans = (comb(n+1, k) - comb(L+R, k-1)) % mod
    print(ans)