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:
n = int(input())
a = list(map(int,input().split()))
search = [-1 for i in range(n)]
l,r = -1,-1
for i,x in enumerate(a):
    if search[x-1] == -1:
        search[x-1] = i
    else:
        l = search[x-1]
        r = i
#print(l,r)
p = 10**9 + 7

#前計算
comb1 = [0 for _ in range(n+1)]
comb1[0] = n+1
x = n+1
for i in range(1,n+1):
    x = (x*(n-i+1))%p
    x = (x*pow(i+1,p-2,p))%p
    comb1[i] = x

comb2 = [0 for _ in range(n+1)]
comb2[0] = 1
x = 1
for i in range(1,l+n-r+1):
    x = (x*(n+l-r-i+1))%p
    x = (x*pow(i,p-2,p))%p
    comb2[i] = x

for i in range(n+1):
    ans = comb1[i]-comb2[i]

    print(ans%p)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC66 D

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))


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

n = I()
a = LI()
cnt = [0]*(n+1)
for i in range(n+1):
    if cnt[a[i]]:
        a = cnt[a[i]]
        b = i+1
        break
    else:
        cnt[a[i]] = i+1

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

for i in range(n+1):
    res = 0
    res = (res + comb(n+1,i+1)) % mod
    res = (res - comb((a-1)+(n+1-b),i)) % mod
    print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC66 D

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def f(a, b):
    if b == 0: 
        return 1
    if b % 2 == 0:
        d = f(a, b // 2)
        return d * d % mod
    if b % 2 == 1:
        return (a * f(a, b - 1)) % mod


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

n = I()
a = LI()
cnt = [0]*(n+1)
for i in range(n+1):
    if cnt[a[i]]:
        a = cnt[a[i]]
        b = i+1
        break
    else:
        cnt[a[i]] = i+1

fact = [1]*(n+2)
invfact = [1]*(n+2)
for i in range(1,n+2):
    fact[i] = i*fact[i-1] % mod
    invfact[i] = invfact[i-1] * f(i,mod-2) % mod

for i in range(n+1):
    res = 0
    res = (res + comb(n+1,i+1)) % mod
    res = (res - comb((a-1)+(n+1-b),i)) % mod
    print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
res = [-1] * (n+1)
for i in range(n+1):
    if res[a[i]] == -1:
        res[a[i]] = i
    else:
        d = a[i]
        l, r = res[d], n-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

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)


for i in range(1, n+2):
    print((cmb(n+1, i, mod)-cmb(l+r, i-1, mod)) % mod)

Submission status: Accepted

Solution code:
class PERM_COMB_MOD():
    # http://drken1215.hatenablog.com/entry/2018/06/08/210000
    def __init__(self, max_n=510000, mod=10**9+7):
        self.fac = [0]*max_n
        self.finv = [0]*max_n
        self.inv = [0]*max_n
        self.fac[0] = self.fac[1] = 1
        self.finv[0] = self.finv[1] = 1
        self.inv[1] = 1
        self.max = max_n
        self.mod = mod
        self._maesyori()

    def _maesyori(self):
        for i in range(2,self.max):
            self.fac[i] = self.fac[i-1] * i % self.mod
            self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.finv[i] = self.finv[i-1] * self.inv[i] % self.mod

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

mod = 10**9+7

n = int(input())
p = PERM_COMB_MOD(n+10)
a = list(map(int, input().split()))
d = {}
l=r=None

for i, ai in enumerate(a):
    if ai in d:
        l = d[ai]
        r = i
        break
    else:
        d[ai] = i

s = l+n-r

for k in range(n+1):
    ans = p.comb(n+1, k+1) - p.comb(s, k)
    print(ans%mod)

Submission status: Accepted

Solution code:
m=10**9+7
n=int(input())
a=list(map(int,input().split()))
b=[0]*(n+1)
for i in range(n+1):
    if b[a[i]]==0:
        b[a[i]]=i+1
    else:
        s=b[a[i]]+n-i-1
        break
p,q=1,1
for i in range(1,n+2):
    p=((p*(n+2-i))*pow(i,m-2,m))%m
    q=(1 if i==1 and s>=i-1 else(0 if s<i-1 else ((q*(s-i+2))*pow(i-1,m-2,m)))%m)
    print((p-q)%m)

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)
i = 1
for j in range(1, n + 1):
    i = i * j % mod
    fac[j] = i
    inv[j] = pow(i, mod - 2, 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:
n=int(input())
a=[int(j) for j in input().split()]
mod=10**9+7
l=[-1]*(n+1)
ind=[]
for i in range(n+1):
    if l[a[i]]!=-1:
        ind=[l[a[i]],i]
        break
    l[a[i]]=i
U = 2*10**5
MOD = 10**9+7
 
fact = [1]*(U+1)
fact_inv = [1]*(U+1)
 
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 k > n:
    return 0
  x = fact[n]
  x *= fact_inv[k]
  x %= MOD
  x *= fact_inv[n-k]
  x %= MOD
  return x

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

Submission status: Accepted

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

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

mod = int(10**9+7) #出力の制限

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

NN = 5*10**5 # 使うデータによって変える
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

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


dic = {}
for i, a in enumerate(A):
    if not a in dic.keys():
        dic[a] = i
    else:
        l, r = dic[a], i
        break

ans = [0]*(N+2)

for k in range(N):
    nCk = cmb(N-1, k)
    ans[k] = (ans[k] + nCk) % mod
    ans[k+1] = (ans[k+1] + 2*nCk - cmb(N-r+l, k)) % mod
    ans[k+2] = (ans[k+2] + nCk) % mod

for k in range(1, N+2):
    print(ans[k])

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
table = [-1] * (N + 1)
d = [0, 0]
mod = 10 ** 9 + 7
class Factorial:
  def __init__(self, n, mod):
    self.f=[1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i=[pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod
f = Factorial(N + 1, mod)
for i in range(N + 1):
  if table[a[i]] != -1:
    d = [table[a[i]], i]
    break
  table[a[i]] = i
for i in range(1, N + 2):
  x = f.combi(N + 1, i)
  w = 0
  if N + d[0] - d[1] >= i - 1:
    w = f.combi(d[0] + N - d[1], i - 1)
  print((x - w) % mod)

Submission status: Accepted

Solution code:
def main():
	n,*a = map(int,open(0).read().split())
	dbl = sum(a)-n*(n+1)//2
	l,r = [i for i,j in enumerate(a) if j==dbl]
	const = n+l-r
	m = n + 1
	mod = 10**9+7
	fac = [1]*(m+1)
	inv = [1]*(m+1)
	for i in range(1,m+1):
		fac[i] = fac[i-1] * i % mod
	inv[-1] = pow(fac[-1], mod - 2, mod)
	for i in range(m-1, -1, -1):
		inv[i] = inv[i+1] * (i+1) % mod
	def cmb(n,r):
		if n<r:return 0
		return fac[n] * inv[n-r] * inv[r] % mod
	for k in range(1,n+2):
		print((cmb(n+1,k)-cmb(const,k-1)) % mod)
if __name__=="__main__":main()

Submission status: Accepted

Solution code:
from collections import Counter
n,*a = map(int,open(0).read().split())
dbl = Counter(a).most_common()[0][0]
l,r = [i for i,j in enumerate(a) if j==dbl]
const = n+l-r
m = n + 1
mod = 10**9+7
fac = [1]*(m+1)
inv = [1]*(m+1)
for i in range(1,m+1):
	fac[i] = fac[i-1] * i % mod
inv[-1] = pow(fac[-1], mod - 2, mod)
for i in range(m-1, -1, -1):
	inv[i] = inv[i+1] * (i+1) % mod
def cmb(n,r):
	if n<r:return 0
	return fac[n] * inv[n-r] * inv[r] % mod
for k in range(1,n+2):
	print((cmb(n+1,k)-cmb(const,k-1)) % mod)

Submission status: Accepted

Solution code:
from collections import Counter

class ModComb:
    def __init__(self, MAX, mod=10 ** 9 + 7):
        fac = [1, 1]
        finv = [1, 1]
        inv = [0, 1]
        for i in range(2, MAX):
            fac.append(fac[i - 1] * i % mod)
            inv.append(mod - inv[mod % i] * (mod // i) % mod)
            finv.append(finv[i - 1] * inv[i] % mod)
        self.fac, self.finv, self.mod = fac, finv, mod

    def nCk(self, n, k):
        if n < k or n < 0 or k < 0:
            return 0
        fac, finv, mod = self.fac, self.finv, self.mod
        return fac[n] * (finv[k] * finv[n - k] % mod) % mod

mod = 10 ** 9 + 7

N, *A = map(int, open(0).read().split())

C = Counter(A)
t = C.most_common()[0][0]
left = A.index(t)
right = A.index(t, left + 1)

mc = ModComb(10 ** 5 + 5, mod=mod)

m = left + N - right
for k in range(1, N + 2):
    print((mc.nCk(N + 1, k) - mc.nCk(m, k - 1)) % mod)

Submission status: Accepted

Solution code:
class ModComb:
    def __init__(self, MAX, mod=10 ** 9 + 7):
        fac = [1, 1]
        finv = [1, 1]
        inv = [0, 1]
        for i in range(2, MAX):
            fac.append(fac[i - 1] * i % mod)
            inv.append(mod - inv[mod % i] * (mod // i) % mod)
            finv.append(finv[i - 1] * inv[i] % mod)
        self.fac, self.finv, self.mod = fac, finv, mod

    def nCk(self, n, k):
        if n < k or n < 0 or k < 0:
            return 0
        fac, finv, mod = self.fac, self.finv, self.mod
        return fac[n] * (finv[k] * finv[n - k] % mod) % mod

mod = 10 ** 9 + 7

N, *A = map(int, open(0).read().split())

mc = ModComb(10 ** 5 + 5, mod=mod)

used = {}
left, right = 0, 0
for i, a in enumerate(A):
    if a in used:
        right = i
        left = used[a]
        break
    used[a] = i

m = left + N - right
for k in range(1, N + 2):
    print((mc.nCk(N + 1, k) - mc.nCk(m, k - 1)) % mod)

Submission status: Accepted

Solution code:
class ModComb:
    def __init__(self, MAX, mod=10 ** 9 + 7):
        fac = [1, 1]
        finv = [1, 1]
        inv = [0, 1]
        for i in range(2, MAX):
            fac.append(fac[i - 1] * i % mod)
            inv.append(mod - inv[mod % i] * (mod // i) % mod)
            finv.append(finv[i - 1] * inv[i] % mod)
        self.fac, self.finv, self.mod = fac, finv, mod

    def nCk(self, n, k):
        if n < k or n < 0 or k < 0:
            return 0
        fac, finv, mod = self.fac, self.finv, self.mod
        return fac[n] * (finv[k] * finv[n - k] % mod) % mod

mod = 10 ** 9 + 7

N, *A = map(int, open(0).read().split())

mc = ModComb(210000, mod=mod)

used = {}
left, right = 0, 0
for i, a in enumerate(A):
    if a in used:
        right = i
        left = used[a]
        break
    used[a] = i

m = left + N - right
for k in range(1, N + 2):
    print((mc.nCk(N + 1, k) - mc.nCk(m, k - 1)) % mod)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class FactInvMOD:
    """ 階乗たくさん使う時用のテーブル準備 """

    def __init__(self, MAX, MOD):
        """ MAX：階乗に使う数値の最大以上まで作る """
        
        MAX += 1
        self.MAX = MAX
        self.MOD = MOD
        # 階乗テーブル
        factorial = [1] * MAX
        factorial[0] = factorial[1] = 1
        for i in range(2, MAX):
            factorial[i] = factorial[i-1] * i % MOD
        # 階乗の逆元テーブル
        inverse = [1] * MAX
        # powに第三引数入れると冪乗のmod付計算を高速にやってくれる
        inverse[MAX-1] = pow(factorial[MAX-1], MOD-2, MOD)
        for i in range(MAX-2, 0, -1):
            # 最後から戻っていくこのループならMAX回powするより処理が速い
            inverse[i] = inverse[i+1] * (i+1) % MOD
        self.fact = factorial
        self.inv = inverse
    
    def nCr(self, n, r):
        """ 組み合わせの数 (必要な階乗と逆元のテーブルを事前に作っておく) """

        if n < r: return 0
        # 10C7 = 10C3
        r = min(r, n-r)
        # 分子の計算
        numerator = self.fact[n]
        # 分母の計算
        denominator = self.inv[r] * self.inv[n-r] % self.MOD
        return numerator * denominator % self.MOD

    def nPr(self, n, r):
        """ 順列 """

        if n < r: return 0
        return self.fact[n] * self.inv[n-r] % self.MOD

    def nHr(self, n, r):
        """ 重複組み合わせ """

        # r個選ぶところにN-1個の仕切りを入れる
        return self.nCr(r+n-1, r)

N = INT()
A = LIST()

# 唯一2つある値を見つける
target = 0
for k, v in Counter(A).items():
    if v == 2:
        target = k
        break
idx = A.index(target)
# 2つある値の1回目と2回目の出現位置の間にいくつ数があるか
btw = A[idx+1:].index(target)

fim = FactInvMOD(N+1, MOD)
for i in range(1, N+2):
    # 部分列の総数 - 重複分
    ans = fim.nCr(N+1, i) - fim.nCr(N-1-btw, i-1)
    print(ans%MOD)

Submission status: Accepted

Solution code:
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2] != 1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,mod):
    x = extgcd(a,mod)[0]
    return (mod + x % mod)% mod
n = int(input())
mod = 10**9 + 7
a = list(map(int,input().split()))
il = [[]for i in range(n)]
for i in range(n+1):
    il[a[i]-1].append(i)
    if len(il[a[i]-1]) > 1:
        p,q = il[a[i]-1]
comb = 1
comb2 = 1
c = p + n - q
for i in range(n+1):
    comb = (((comb*(n+1-i))%mod)*mod_inv(i+1,mod))%mod
    if c >= i:
        print((comb-comb2)%mod)
        comb2 = (((comb2*(c-i))%mod)*mod_inv(i+1,mod))%mod
    else:
        print(comb%mod)

Submission status: Accepted

Solution code:
class Factorial:
    def __init__(self,n,mod):
        self.f=[1]
        self.mod=mod
        for i in range(1,n+1):
            self.f.append(self.f[-1]*i%mod)
        self.i=[pow(self.f[-1],mod-2,mod)]
        for i in range(1,n+1)[::-1]:
            self.i.append(self.i[-1]*i%mod)
        self.i.reverse()
    def factorial(self,i):
        return self.f[i]
    def ifactorial(self,i):
        return self.i[i]
    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*
M=10**9+7
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,M).comb
for k in range(n+1):print((c(n+1,k+1)-c(l+r,k))%M)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
dup = c.most_common()[0][0]

id = [i for i, x in enumerate(a) if x == dup]
L = id[0]
R = id[1]

MOD = 10**9 +7
factorial = [1]
inverse = [1]

for i in range(1, N+2):
    factorial.append(factorial[i-1] * i % MOD)
    inverse.append(pow(factorial[i], MOD-2, MOD))
    

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

print(N)
for i in range(2, N+2):
    ans = (C(N+1, i) - C(L+(N-R), i-1)) % MOD
    print(ans)

Submission status: Accepted

Solution code:
mod=10**9+7
n=int(input())
a=list(map(int,input().split()))
d=sum(a)-sum(range(n+1))
i1=a.index(d)
i2=i1+1+a[i1+1:].index(d)

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)

h1=[1,1]
h2=[1,1]
iinverse=[0,1]
for i in range(2,n-i2+i1+1):
    h1.append((h1[-1]*i)%mod)
    iinverse.append((-iinverse[mod % i]*(mod//i))%mod)
    h2.append((h2[-1]*iinverse[-1])%mod)

for i in range(n+1):
    print((g1[n+1]*g2[i+1]*g2[n-i]%mod-(h1[n-i2+i1]*h2[i]*h2[n-i2+i1-i] if n-i2+i1>=i else 0)%mod)%mod)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))

MOD = 10**9 + 7
MAX = n + 5
fact = [1 for _ in range(MAX)]
finv = [1 for _ in range(MAX)]
for i in range(2, MAX):
    fact[i] = fact[i - 1] * i % MOD
    finv[i] = pow(fact[i], MOD-2, MOD)

def comb(n: int, k: int) -> int:
    if n < k or n < 0 or k < 0:
        return 0
    return (fact[n] * finv[k] % MOD) * finv[n-k] % MOD


d = defaultdict(list)
for i in range(n+1):
    d[a[i]].append(i)
    if len(d[a[i]]) == 2:
        i1, i2 = sorted(d[a[i]])
        m1 = i1
        m2 = i2 - i1 - 1
        m3 = n - i2

# print((i1, i2), m1, m2, m3)
ans = []
for k in range(1, n+2):
    ansi = comb(n+1, k) - comb(m1+m3, k-1)
    ansi = (ansi + MOD) % MOD
    ans.append(ansi)

for ansi in ans:
    print(ansi)

Submission status: Accepted

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

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

    kai = [1] * (N+2)
    MOD = int(1e9) + 7
    for i in range(1, N+2):
        kai[i] *= kai[i-1] * i
        kai[i] %= MOD
    # print(kai)
    dic = {}
    for i in range(N+1):
        if a[i] in dic:
            left = dic[a[i]]
            right = N - i
        else:
            dic[a[i]] = i
    
    for i in range(1, N+2):
        tmp = kai[N+1] * pow(kai[N+1-i], MOD-2, MOD) * pow(kai[i], MOD-2, MOD)
        tmp %= MOD
        if i - 1 <= left + right:
            tmp2 = kai[left+right] * pow(kai[left+right+1-i], MOD-2, MOD) * pow(kai[i-1], MOD-2, MOD)
            tmp2 %= MOD
            tmp = (tmp + MOD - tmp2) % MOD
        print(tmp)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
mod = 10**9 + 7

IND = [-1] * (n+1)
for i in range(n+1):
    if IND[A[i]] != -1:
        bet = i - IND[A[i]] - 1
        break
    else:
        IND[A[i]] = i

# print("bet", bet)

M = [1]
mul = 1
for i in range(1, n+3):
    mul = (mul * i) % mod
    M.append(mul)

# print(M[:10])

def inv(a, p):
    pp = p
    p -= 2
    ans = 1
    mul = a
    while p > 0:        
        if p % 2 == 1:
            ans = (ans * mul) % pp
            #print(ans, p)
        mul = (mul ** 2) % pp
        p //= 2
    return ans % pp

#print(inv(3, 13))

I = [1] + [inv(i, mod) for i in range(1, n+3)]
MI = [1]
mul = 1
for i in range(1, n+3):
    mul = (mul * I[i]) % mod
    MI.append(mul)

# print(MI[:10])
# print(I[:10])

D = [0, 0, 1]
for i in range(3, n+3):
    D.append((D[-1] * (n+1-(i-1)) * I[i-2]) % mod)

def C(x, y):
    if x < y or x < 0 or y < 0:
        return 0
    elif x == 0 or y == 0:
        return 1
    else:
        ans = M[x]
        ans = (ans * MI[y]) % mod
        ans = (ans * MI[x-y]) % mod
        return ans

#print(C(6,3))

for k in range(1, n+1):
    ans = C(n-1, k)
    ans = (ans - C(n-1-bet, k-1)) % mod
    ans = (ans + 2 * C(n-1, k-1)) % mod
    ans = (ans + C(n-1, k-2)) % mod
    print(ans)
print(1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    div = pow(10, 9) + 7

    # print(A)
    d = dict()
    for i in range(N+1):
        if A[i] not in d: d[A[i]] = [i]
        else: d[A[i]].append(i)

    i1, i2 = 0, 0
    for a in d:
        if len(d[a]) == 2:
            i1, i2 = d[a]
            break
    M = i1 + N-i2
    # print(N, i1, i2)

    # N+1_C_l
    pow1 = [1, 1]
    for i in range(2, N+2):
        pow1.append(pow1[-1]*i % div)

    pow2 = []
    j = powquick(pow1[-1], div-2, div)
    pow2.append(j)
    for i in range(N, -1, -1):
        pow2.append(pow2[-1]*(i+1) % div)
    pow2 = pow2[::-1]


    # pow2 = [1, 1]
    # for i in range(2, N+2):
    #     j = powquick(i, div-2, div)
    #     pow2.append(pow2[-1]*j % div)

    # pow2 = [1, 1]
    # inv = [0, 1]
    # for i in range(2, N+2):
    #     inv.append((-inv[div % i]) *(div // i) % div)
    #     pow2.append(pow2[-1] * inv[-1] % div)

    for l in range(1, N+2):
        v = (pow1[N+1]*pow2[l]*pow2[N+1-l]) % div
        # print(v)
        if l <= M+1:
            v -= (pow1[M]*pow2[l-1]*pow2[M-l+1]) % div
        print(v % div)

def powquick(x, y, div):
        if y == 0: return 1
        if y == 1: return x % div
        if y % 2 == 0: return pow(powquick(x, y // 2, div), 2) % div
        if y % 2 == 1: return pow(powquick(x, y// 2, div), 2) * x % div


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    div = pow(10, 9) + 7

    # print(A)
    d = dict()
    for i in range(N+1):
        if A[i] not in d: d[A[i]] = [i]
        else: d[A[i]].append(i)

    i1, i2 = 0, 0
    for a in d:
        if len(d[a]) == 2:
            i1, i2 = d[a]
            break
    M = i1 + N-i2
    # print(N, i1, i2)

    # N+1_C_l
    pow1 = [1, 1]
    for i in range(2, N+2):
        pow1.append(pow1[-1]*i % div)

    # pow2 = [1, 1]
    # for i in range(2, N+2):
    #     j = powquick(i, div-2, div)
    #     pow2.append(pow2[-1]*j % div)

    pow2 = [1, 1]
    inv = [0, 1]
    for i in range(2, N+2):
        inv.append((-inv[div % i]) *(div // i) % div)
        pow2.append(pow2[-1] * inv[-1] % div)

    for l in range(1, N+2):
        v = (pow1[N+1]*pow2[l]*pow2[N+1-l]) % div
        # print(v)
        if l <= M+1:
            v -= (pow1[M]*pow2[l-1]*pow2[M-l+1]) % div
        print(v % div)

# def powquick(x, y, div):
#         if y == 0: return 1
#         if y == 1: return x % div
#         if y % 2 == 0: return pow(powquick(x, y // 2, div), 2) % div
#         if y % 2 == 1: return pow(powquick(x, y// 2, div), 2) * x % div


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    div = pow(10, 9) + 7

    # print(A)
    d = dict()
    for i in range(N+1):
        if A[i] not in d: d[A[i]] = [i]
        else: d[A[i]].append(i)

    i1, i2 = 0, 0
    for a in d:
        if len(d[a]) == 2:
            i1, i2 = d[a]
            break
    M = i1 + N-i2
    # print(N, i1, i2)

    # N+1_C_l
    pow1 = [1, 1]
    for i in range(2, N+2):
        pow1.append(pow1[-1]*i % div)

    # pow2 = [1, 1]
    # for i in range(2, N+2):
    #     j = powquick(i, div-2, div)
    #     pow2.append(pow2[-1]*j % div)

    pow2 = [1, 1]
    inv = [0, 1]
    for i in range(2, N+2):
        inv.append((-inv[div % i]) *(div // i) % div)
        pow2.append(pow2[-1] * inv[-1] % div)

    for l in range(1, N+2):
        v = (pow1[N+1]*pow2[l]*pow2[N+1-l]) % div
        # print(v)
        if l <= M+1:
            v -= (pow1[M]*pow2[l-1]*pow2[M-l+1]) % div
        print(v % div)

def powquick(x, y, div):
        if y == 0: return 1
        if y == 1: return x % div
        if y % 2 == 0: return pow(powquick(x, y // 2, div), 2) % div
        if y % 2 == 1: return pow(powquick(x, y// 2, div), 2) * x % div


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b, c = LI()
    print(min(a+b,b+c,c+a))
    return

#B
def B():
    def check(s):
        x = len(s)
        if x % 2:
            return False
        if s[:x // 2] == s[x // 2:]:
            return True
    s = S()
    for i in range(1,len(s)):
        if check(s[: - i]):
            print(len(s) - i)
            return

#C
def C():
    II()
    a = LI()
    ans1 = a[-1::-2]
    ans2 = a[-2::-2][::-1]
    ans = ans1 + ans2
    print(*ans)
    return

#D
def D():
    def power_func(a, b):
        """ a^b mod p を求める """
        """ bを2進数分解して高速累乗 """

        if b == 0: return 1
        if b % 2 == 0:
            d = power_func(a, b // 2)
            return d * d % mod
        if b % 2 == 1:
            return (a * power_func(a, b - 1)) % mod

    def combination_mod(n, k):
        """ power_funcを用いて(nCk) mod p を求める """ 
        """ nCk = n!/((n-k)!k!)を使用 """

        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        a = factorial[n]
        b = invfactorial[k]
        c = invfactorial[n - k]
        return (a * b * c) % mod

    n = II() + 1
    a = LI()
    d = [0] * n
    for i in range(n):
        if d[a[i]]:
            a = d[a[i]] - 1
            c = n - i - 1
            break
        d[a[i]] = i + 1
    factorial = [1] * (n + 1)
    invfactorial = [1] * (n + 1)
    for i in range(1, n + 1):
        factorial[i] = (factorial[i - 1] * i) % mod
        invfactorial[i] = power_func(i, mod - 2) * invfactorial[i-1] % mod
    for i in range(1, n + 1):
        ans = combination_mod(n, i) - combination_mod(a + c, i - 1)
        print(ans%mod)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
M = [-1]*n
p, q = 0, 1
for i in range(n+1):
  A = As[i]
  A = A-1
  if not(M[A] < 0):
    p, q = M[A], i
    break
  M[A] = i
#print(p, q)

MAX = 200000
MOD = 1000000007
fac = [0]*MAX
finv = [0]*MAX
inv = [0]*MAX
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, MAX):
  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 COM(n, k):
  if n < 0 or k < 0 or n < k:
    return 0
  return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD

for k in range(1, n+2):
  # C_{n+1}_k-C_{n-1+p-q}_{k-1}
  r1 = COM(n+1, k)
  r2 = COM(n+p-q, k-1)
  r = (r1-r2)%MOD
  print(r)

Submission status: Accepted

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

mod = 10 ** 9 + 7
fac = [1]
inv = [1]
for i in range(1, n + 2) :
    fac.append(fac[-1] * i % mod)
    inv.append(pow(fac[-1], mod-2, mod))

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

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

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