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()))
d=dict()
#コンビネーション逆元
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**6
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 )

for i in range(n+1):
    if a[i] not in d:
        d[a[i]]=i
    else:
        idx1=d[a[i]]
        idx2=i
        break
for i in range(1,n+2):
    ans=cmb(n+1,i,mod)
    if n-idx2+idx1<i-1:
        print(ans)
    else:
        ans=(ans-cmb(n-idx2+idx1,i-1,mod)+mod)%mod
        print(ans%mod)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
T = [-1]*(N+1)
for i in range(N+1):
    if T[A[i]]>=0:
        P1 = T[A[i]]
        P2 = i
        break
    T[A[i]]=i

Q1 = P1
Q2 = P2-P1-1
Q3 = N-P2

MAXN = (N+1)+5
p = 10**9+7
MOD = p
f = [1]
for i in range(MAXN):
    f.append(f[-1] * (i+1) % MOD)
def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod


for K in range(1,N+2):
    if K==1:
        print(N)
        continue
    X = nCr(N+1,K,p)
    if Q1+Q3>=K-1:
        Y = nCr(Q1+Q3,K-1,p)
    else:
        Y = 0
    if X-Y<0:
        X+=p
    print((X-Y)%p)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
kasanari = Counter(a).most_common()[0][0]
mod = 10**9+7
"""
2 3 1 4 5 6 1 7 8 9
Y Y C C C C C X X X
k個選ぶ. 全部 nCk-3Ck
"""
basho = []
for cnt, i in enumerate(a):
	if i == kasanari:
		basho.append(cnt)

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

N = 2*10**5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N+1):
	fact.append((fact[-1]*i)%mod)
	inv.append((-inv[mod%i]*(mod//i))%mod)
	factinv.append((factinv[-1]*inv[-1])%mod)

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

SIZE = 10 ** 5 + 5
fact = [0] * SIZE
inv = [0] * SIZE
finv = [0] * SIZE
fact[0], fact[1] = 1, 1
inv[1] = 1
finv[0], finv[1] = 1, 1
for i in range(2, SIZE):
    fact[i] = fact[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod


def nCr(n, r):
    if n < 0 or r < 0 or n < r:
        return 0
    return fact[n] * (finv[r] * finv[n - r] % mod) % mod


from collections import Counter


n = int(input())
A = list(map(int, input().split()))
counterA = Counter(A)
duplicate = 0
for num, count in counterA.items():
    if count == 2:
        duplicate = num
        break

duplicate_pos = []
for i, a in enumerate(A):
    if a == duplicate:
        duplicate_pos.append(i)

first, second, *_ = duplicate_pos

for k in range(1, n+2):
    base = nCr(n+1, k)
    duplicate_pattern = nCr(first + n-second, k-1)
    print((base - duplicate_pattern) % mod)

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def modinv2(x,r):
  return pow(x,r-2,r)

def comb(n,k):
    ret = fact[n]*modinv2(fact[n-k]*fact[k],R)%R
    #ddprint(f"comb {n=} {k=} {ret=}")
    return ret

n = inn()
a = inl()
fact = [1]*(n+1)
for i in range(1,n+1):
    fact[i] = (fact[i-1]*i)%R
ddprint(fact)
c = [-1]*(n+1)
for m in range(n+1):
    x = a[m]
    if c[x]>=0:
        i = c[x]
        j = m+1
        break
    c[x] = m+1
#ddprint(f"{n=} {i=} {j=}")
print(n) # k=1
for k in range(2,n):
    #ddprint(f"{k=}")
    v0 = comb(n-1,k) if k<=n-1 else 0
    #ddprint(f"{v0=}")
    v2 = comb(n-1,k-2) if k>=2 else 0
    #ddprint(f"{v2=}")
    v1 = 2*comb(n-1,k-1) - \
         (0 if n-j+i<k-1 else comb(n-j+i,k-1))
    #ddprint(f"{k=} {v0=} {v1=} {v2=}")
    print((v0+v1+v2)%R)
if n>1:
    print(n if j==i+1 else n+1) # k=n
print(1) # k=n+1

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n=int(input())
a=list(map(int,input().split()))
s=set()
for i in range(n+1):
    if a[i] not in s:
        s.add(a[i])
    else:
        q=i
p=a.index(a[q])
q=n-q
Mod=10**9+7
M=n+2 #書き換え
fac=[0]*M
finv=[0]*M
inv=[0]*M
#finvに逆元、facに階乗のmod
def COMinit():
  fac[0]=fac[1]=1
  finv[0]=finv[1]=1
  inv[1]=1
  for i in range(2,M):
    fac[i]=(fac[i-1]*i%Mod)%Mod
    inv[i]=Mod-inv[Mod%i]*(Mod//i) %Mod
    finv[i]=(finv[i-1]*inv[i]%Mod)%Mod
def COM(n,k):
  if n<k:
    return 0
  if n<0 or k<0:
    return 0
  return (fac[n]*(finv[k]*finv[n-k]%Mod)%Mod)%Mod

COMinit()
for i in range(1,n+2):
    print((COM(n+1,i)-COM(p+q,i-1))%Mod)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=pow(10,9)+7
#ans=[int(input()) for _ in range(n+1)]
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 )
# 二つあるやつの左の方の左にl個、右のやつの右のほうにr個数字がある
f=[-1]*(n+1)
l,r=0,0
for i in range(n+1):
  ai=a[i]
  if f[ai]==-1:
    f[ai]=i
  else:
    l=f[ai]
    r=n-i
    break
for k in range(1,n+2):
  a=cmb(n+1,k,mod)
  if l+r>=k-1:
	  a-=cmb(l+r,k-1,mod)
  #print(a==ans[k-1],a,ans[k-1])
  print(a%mod)

Submission status: Accepted

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

b = sorted(a[::])
for i in range(n):
    if b[i] == b[i+1]:
        d = b[i]

l, r = -1, -1
for i in range(n+1):
    if l < 0:
        if a[i] == d:
            l = i
    else:
        if a[i] == d:
            r = i

f = [1 for _ in range(n+2)]
f_inv = [1 for _ in range(n+2)]
mod = 10 ** 9 + 7
for i in range(1, n+2):
    f[i] = f[i-1] * i % mod
    f_inv[i] = pow(f[i], mod-2, mod)

def comb(n, k):
    return f[n] * f_inv[k] * f_inv[n-k] % mod


for k in range(1, n+2):
    ans = 0

    if k >= 2:
        ans += comb(n-1, k-2)
        ans %= mod

    if n - 1 >= k:
        ans += comb(n-1, k)
        ans %= mod

    if n >= k:
        ans += 2 * comb(n-1, k-1)
        ans %= mod

    if n + l - r >= k - 1:
        ans -= comb(n+l-r, k-1)
        ans %= mod

    print(ans)

Submission status: Accepted

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

dub=[-1]*(n+1)
duble=[]

for i,aa in enumerate(a):
    if dub[aa]==-1:
        dub[aa]=i
    else:
        duble=[dub[aa],i]


def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
p = 10**9+7 #割る数
N = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

#cmb(n, c, p)

duble.sort()
left=duble[0]
right=duble[1]
#print(left,middle,right)
for i in range(1,n+2):
    ans=cmb(n+1,i,p)-cmb(n+1-2-(right-1-left),i-1,p)
    print(ans%p)

Submission status: Accepted

Solution code:
class cmbs(object):
    def __init__(self, mod):
        self.mod = mod
        self.g1 = [1, 1]
        self.g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, 10 ** 6 + 1):
            self.g1.append((self.g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            self.g2.append((self.g2[-1] * inverse[-1]) % mod)

    def cmb(self, n, r):
        if n > 10 ** 6:
            return self.cmbl(n, r)
        return self.cmbr(n, r)

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

    def cmbl(self, n, r):
        t = 1
        r = min(r, n-r)
        for i in range(n - r + 1, n + 1):
            t = t * i % self.mod
        return t * self.g2[r] % self.mod
def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = {}
    dif = 0
    for i, v in enumerate(A):
        if v not in d:
            d[v] = i
        else:
            dif = i - d[v]
            break
    mod = 10**9 + 7
    c = cmbs(mod)
    print(N)
    if N == 1:
        print(N)
        return
    if dif == 1:
        for i in range(2, N):
            if N-1 >= i:
                r = c.cmb(N-1, i-2) + c.cmb(N-1, i-1) + c.cmb(N-1, i)
            else:
                r = c.cmb(N-1, i-2) + c.cmb(N-1, i-1)
            print(r % mod)
        print(N)
        print(1)
        return
    for i in range(2, N):
        r = c.cmb(N-1, i-2) + c.cmb(N-1, i-1)*2 - c.cmb(N-dif, i-1) + c.cmb(N-1, i)
        print(r % mod)
    print(N+1)
    print(1)
main()

Submission status: Accepted

Solution code:
class nCrMod():
    def __init__(self, mod):
        self.mod = mod
        self.fac = [1, 1]
        self.finv = [1, 1]
        self.inv = [0, 1]

    def prep(self, n):
        mod = self.mod
        f, fi = self.fac[-1], self.finv[-1]
        for i in range(len(self.fac), n + 1):
            fn = f * i % mod
            v = -self.inv[mod % i] * (mod // i) % mod
            fin = fi * v % mod
            f, fi = fn, fin
            self.fac.append(f)
            self.finv.append(fi)
            self.inv.append(v)

    def __call__(self, n, r):
        if len(self.fac) <= n:
            self.prep(n)
        return self.fac[n] * self.finv[r] * self.finv[n - r] % self.mod

def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = {}
    f, s = 0, 0
    for i, a in enumerate(A):
        if a in d:
            break
        d[a] = i
    f, s = d[a], i
    mod = 10**9 + 7
    nCr = nCrMod(mod)
    for i in range(1, N + 2):
        if i > N + 1 - (s - f):
            print(nCr(N + 1, i))
        else:
            print((nCr(N + 1, i) - nCr(N - (s - f), i - 1)) % mod)
main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


class CmbMod:
    def __init__(self, n, p):
        """
        二項係数nCr(n個の区別できるものからr個のものを選ぶ組み合わせの数)をpで割った余りを求める
        """
        self.n = n
        self.p = p
        self.fact = [1, 1]
        self.factinv = [1, 1]
        self.inv = [0, 1]

    def cmb_mod(self, n, r):
        """
        二項係数nCr(mod p)をO(r)にて計算。nが大きいがrは小さい時に使用。
        """
        numer, denom = 1, 1
        for i in range(r):
            numer = (numer * (n - i)) % self.p
            denom = (denom * (i + 1)) % self.p
        return (numer * pow(denom, self.p - 2, self.p)) % self.p

    def prep(self):
        """
        二項係数nCr(mod p)をO(1)で求める為の前処理をO(N)にて実行。
        """
        for i in range(2, self.n + 1):
            self.fact.append((self.fact[-1] * i) % self.p)
            self.inv.append((-self.inv[self.p % i] * (self.p // i)) % self.p)
            self.factinv.append((self.factinv[-1] * self.inv[-1]) % self.p)

    def cmb_mod_with_prep(self, n, r):
        """
        二項係数nCr(mod p)をO(1)で求める。事前にprepを実行する事。
        """
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return self.fact[n] * self.factinv[r] * self.factinv[n - r] % self.p


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    D = Counter(A)
    dup = 0
    for k, v in D.items():
        if v == 2:
            dup = k
            break

    flg = 1
    right, left = 0, 0
    for i in range(n + 1):
        if flg and A[i] == dup:
            left = i
            flg ^= 1
        elif A[i] == dup:
            right = i
            break

    remain = n - (right - left)
    cmb = CmbMod(n + 1, mod)
    cmb.prep()
    for i in range(1, n + 2):
        res = cmb.cmb_mod_with_prep(n + 1, i) - cmb.cmb_mod_with_prep(remain, i - 1)
        print(res % mod)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    D = Counter(A)

    for k, v in D.items():
        if v == 2:
            db = k
            break

    flg = 1
    right, left = 0, 0
    for i in range(n + 1):
        if flg and A[i] == db:
            left = i
            flg ^= 1
        elif A[i] == db:
            right = i
            break

    length = n - (right - left)

    # 二項係数(mod p)をO(1)で求める
    def cmb(n, r, mod):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n - r] % mod

    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]

    for i in range(2, n + 2):
        fact.append((fact[-1] * i) % mod)
        inv.append((-inv[mod % i] * (mod // i)) % mod)
        factinv.append((factinv[-1] * inv[-1]) % mod)

    def cmb_mod(n, r, mod):
        x, y = 1, 1
        for i in range(r):
            x = (x * (n - i)) % mod
            y = (y * (i + 1)) % mod
        return (x * pow(y, mod - 2, mod)) % mod

    for i in range(1, n + 2):
        res = cmb(n + 1, i, mod) - cmb(length, i - 1, mod)
        print(res % mod)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
s = set()
for i in range(n+1):
    if a[i] in s:
        last = n-i
        t = a[i]
        break
    s.add(a[i])
for i in range(n+1):
    if a[i] == t:
        fir = i
        break 
soto = fir + last
class Combination:
    """
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

comb = Combination(2*10**5+10)

for i in range(1,n+2):
    rm = 0
    if i <= soto+1:
        rm = comb(soto,i-1)
    print((comb(n+1,i) - rm)%mod)

Submission status: Accepted

Solution code:
from math import factorial

n = int(input())
a = list(map(int,input().split()))
se = set()
mod = 10**9+7

for i,j in enumerate(a):
    if j in se:
        two = i
    else:
        se.add(j)

one = a.index(a[two])

def prepare(n, MOD):
    facts = [1]*(n+1)
    for i in range(1, n+1):
        facts[i] = facts[i-1]*i%MOD
    invs = [1]*(n+1)
    invs[n] = pow(facts[n], MOD-2, MOD)
    for i in range(0, n)[::-1]:
        invs[i] = invs[i+1] * (i+1) % MOD
    return facts, invs

facts,invs = prepare(n+1,mod)

def make_combi(facts,invs,n,r,mod):
    return facts[n] * invs[r] * invs[n - r] %mod

for i in range(1,n+2):
    point = make_combi(facts,invs,n+1,i,mod)
    if one+n-two >= i-1:
        point -= make_combi(facts,invs,one+n-two,i-1,mod)
    print(point%mod)

Submission status: Accepted

Solution code:
def cmb(n, r, p):
  if (r < 0) or (n < r):
    return 0
  r = min(r, n - r)
  return fac[n] * finv[r] * finv[n-r] % p

M = pow(10,6)+10
MOD = pow(10,9)+7

fac = [-1]*(M+1); fac[0] = 1; fac[1] = 1
finv = [-1]*(M+1); finv[0] = 1; finv[1] = 1
inv = [-1]*(M+1); inv[0] = 0; inv[1] = 1
for i in range(2,M+1):
  fac[i] = fac[i-1]*i%MOD
  inv[i] = MOD - inv[MOD%i]*(MOD//i)%MOD
  finv[i] = finv[i-1]*inv[i]%MOD

#print(cmb(5, 4, MOD))


N = int(input())
A = list(map(int,input().split()))
dic = {}
for i in range(N+1):
  if A[i] not in dic:
    dic[A[i]] = i
  else:
    left = dic[A[i]]
    right = i
    break
#print(dic)
#print(left,right)

for i in range(1,N+2): #1個からN+1個選ぶ
  kosu = i
  total = cmb(N+1,kosu,MOD)
  outside = left + N-right
  dob = cmb(outside,kosu-1,MOD)
  ans = (total - dob)%MOD
  print(ans)

Submission status: Accepted

Solution code:
class Calc:
    def __init__(self, max_value, mod):
        """combination(max_value, all)"""
        fact = [-1] * (max_value + 1)
        fact[0] = 1
        fact[1] = 1
        for x in range(2, max_value + 1):
            fact[x] = x * fact[x - 1] % mod

        invs = [1] * (max_value + 1)
        invs[max_value] = pow(fact[max_value], mod - 2, mod)
        for x in range(max_value - 1, 0, -1):
            invs[x] = invs[x + 1] * (x + 1) % mod

        self.fact = fact
        self.invs = invs
        self.mod = mod

    def nCr(self, n, r):
        r = min(n - r, r)
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        return self.fact[n] * self.invs[r] * self.invs[n - r] % self.mod

    def nHr(self, n, r):
        return self.nCr(n - 1 + r, r)


def main():
    MOD = 10 ** 9 + 7

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

    B = [-1] * (N + 1)  # はじめて出現する位置(0-ind)
    d = None  # 重複要素の出現位置(2点)
    for i, x in enumerate(A):
        if ~B[x]:
            d = B[x], i
            break
        B[x] = i

    # 重複要素L,R(d[0],d[1]),一度だけ出現する数の列X,Y,Z(|X|>=0,|Y|>=0,|Z|>=0)
    # X...LY...RZ...
    # 重複計上: choose(N+1,K)
    # ここから重複分を差し引く
    # LRを含む個数
    # 0個: 重複計上なし
    # 1個: (X,L,YZ)と(XY,R,Z)が一致する場合に重複計上になる==(X,L/R,Z)
    # ->
    # #==sum(choose(X,a)*choose(Z,K-1-a)),(0<=a<=K-1)
    # 見方を変える
    # Yの区間以外からK個選ぶ
    # ただし,Yの区間外のうちLRから0個取るものと2個取るものを差し引く
    # 残りは(X,L/R,Z)を二重計上したものなので,半分にする
    # (choose(N+1-Y,K) - choose(N+1-2-Y,K) - choose(N+1-2-Y,K-2)) // 2
    # <editorial>
    # LRから1個,XZからK-1個取るchoose(XZ,K-1)=choose(N+1-2-Y,K-1)でよい
    # 2個: 重複計上なし

    calc = Calc(max_value=N + 1, mod=MOD)
    ans = []
    Y = d[1] - 1 - d[0]
    for k in range(1, N + 2):
        ans.append((calc.nCr(N + 1, k) - calc.nCr(N + 1 - 2 - Y, k - 1)) % MOD)

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class Calc:
    def __init__(self, max_value, mod):
        """combination(max_value, all)"""
        fact = [-1] * (max_value + 1)
        fact[0] = 1
        fact[1] = 1
        for x in range(2, max_value + 1):
            fact[x] = x * fact[x - 1] % mod

        invs = [1] * (max_value + 1)
        invs[max_value] = pow(fact[max_value], mod - 2, mod)
        for x in range(max_value - 1, 0, -1):
            invs[x] = invs[x + 1] * (x + 1) % mod

        self.fact = fact
        self.invs = invs
        self.mod = mod

    def nCr(self, n, r):
        r = min(n - r, r)
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        return self.fact[n] * self.invs[r] * self.invs[n - r] % self.mod

    def nHr(self, n, r):
        return self.nCr(n - 1 + r, r)


def main():
    MOD = 10 ** 9 + 7

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

    B = [-1] * (N + 1)  # はじめて出現する位置(0-ind)
    d = None  # 重複要素の出現位置(2点)
    for i, x in enumerate(A):
        if ~B[x]:
            d = B[x], i
            break
        B[x] = i

    # 重複要素L,R(d[0],d[1]),一度だけ出現する数の列X,Y,Z(|X|>=0,|Y|>=0,|Z|>=0)
    # X...LY...RZ...
    # 重複計上: choose(N+1,K)
    # ここから重複分を差し引く
    # LRを含む個数
    # 0個: 重複計上なし
    # 1個: (X,L,YZ)と(XY,R,Z)が一致する場合に重複計上になる==(X,L/R,Z)
    # ->
    # #==sum(choose(X,a)*choose(Z,K-1-a)),(0<=a<=K-1)
    # 見方を変える
    # Yの区間以外からK個選ぶ
    # ただし,Yの区間外のうちLRから0個取るものと2個取るものを差し引く
    # 残りは(X,L/R,Z)を二重計上したものなので,半分にする
    # (choose(N+1-Y,K) - choose(N+1-2-Y,K) - choose(N+1-2-Y,K-2)) // 2
    # 2個: 重複計上なし

    calc = Calc(max_value=N + 1, mod=MOD)
    ans = []
    Y = d[1] - 1 - d[0]
    div2 = pow(2, MOD - 2, MOD)
    for k in range(1, N + 2):
        t = (
                    calc.nCr(N + 1, k)
                    - (
                            calc.nCr(N + 1 - Y, k)
                            - calc.nCr(N + 1 - 2 - Y, k)
                            - calc.nCr(N + 1 - 2 - Y, k - 2)
                    ) * div2 % MOD
            ) % MOD
        ans.append(t)

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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




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

lst = [-1] * (N + 1)
for i in range(N + 1):
    a = A[i]
    if lst[a] == -1:
        lst[a] = i
    else:
        left = lst[a]
        right = i
        break
# print (left, right)
F = Factorial(N + 1, MOD).comb

for i in range(1, N + 2):
    if left + N - right >= i - 1:
        tmp = F(N + 1, i) - F(left + (N- right), i - 1)
    else:
        tmp = F(N + 1, i)
    print (tmp % MOD)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

MAX = 10**5+100
MOD = 10**9+7
fact = [0]*MAX #fact[i]: i!
inv = [0]*MAX #inv[i]: iの逆元
finv = [0]*MAX #finv[i]: i!の逆元
fact[0] = 1
fact[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
    
for i in range(2, MAX):
    fact[i] = fact[i-1]*i%MOD
    inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD
    finv[i] = finv[i-1]*inv[i]%MOD

def C(n, r):
    if n<r:
        return 0
    if n<0 or r<0:
        return 0
    return fact[n]*(finv[r]*finv[n-r]%MOD)%MOD

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

for i in range(n+1):
    idx[a[i]].append(i)

for i in range(1, n+1):
    if len(idx[i])==2:
        f = idx[i][0]
        s = idx[i][1]
        break

for k in range(1, n+2):
    print((C(n-1, k)+2*C(n-1, k-1)-C(n+f-s, k-1)+C(n-1, k-2))%MOD)

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

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

n = int(input())
lis = list(map(int,input().split()))
li = [-1] * n

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(n+1):
    if li[lis[i]-1] != -1:
        left = li[lis[i]-1]+1
        right = i+1
        break
    else:li[lis[i]-1] = i

for k in range(1,n+2):
  #     print(nCr(n+1,k,mod),nCr(left+n-right,k-1,mod))
    ans = cmb(n+1,k,mod)-cmb(left+n-right,k-1,mod)
    if ans < 0:
        ans += 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

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

n = int(input())
lis = list(map(int,input().split()))
li = [-1] * n

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(n+1):
    if li[lis[i]-1] != -1:
        left = li[lis[i]-1]+1
        right = i+1
        break
    else:li[lis[i]-1] = i

for k in range(1,n+2):
  #     print(nCr(n+1,k,mod),nCr(left+n-right,k-1,mod))
    ans = cmb(n+1,k,mod)-cmb(left+n-right,k-1,mod)
    if ans < 0:
        ans += mod
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7
comb = Combination(N + 100)

cntA = Counter(A)
X = [a for a, c in cntA.items() if c == 2][0]
l, r = [i for i, a in enumerate(A) if a == X]

ans = [N]
for leng in range(2, N + 2):
    M = comb.ncr(N + 1, leng)

    L = l + (N - r)
    D = comb.ncr(L, leng - 1)
    ans.append((M - D) % MOD)

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
def cmb(n, r, p):
	if (r<0) or (n<r):
		return 0
	r=min(r, n-r)
	return fact[n]*factinv[r]*factinv[n-r]%p

p=10**9+7
N=10**6
fact=[1,1]
factinv=[1,1]
inv=[0,1]

for i in range(2, N+1):
	fact.append((fact[-1]*i)%p)
	inv.append((-inv[p%i]*(p//i)%p))
	factinv.append((factinv[-1]*inv[-1])%p)


N=int(input())
A=list(map(int,input().split()))
index=[-1]*N
for i,a in enumerate(A):
    if index[a-1]!=-1:
        dup_first,dup_second=index[a-1],i
        break
    index[a-1]=i

mod=10**9+7
n=dup_first
m=N-dup_second
l=n+m
for k in range(1,N+2):
    if k==1:
        print(N)
        continue
    ans=(cmb(N+1,k,mod)-cmb(l,k-1,mod))%mod
    print(ans)