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: Time Limit Exceeded

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(1000000000)
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 math.factorial(n)*pow(math.factorial(r),mod-2,mod)*pow(math.factorial(n-r),mod-2,mod) % mod
n = I()
a = LI()
c = list(Counter(a).items())
c.sort(key = itemgetter(1),reverse = True)
x = c[0][0]
lst = []
for i in range(n+1):
    if a[i] == x:
        lst.append(i+1)
a,b = lst[0],lst[1]
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: Time Limit Exceeded

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):
    ret = comb(n, j)
    for k in range(j):
        ret = (ret - comb(l, k) * comb(r, j - k - 1)) % mod
    print(ret)

Submission status: Time Limit Exceeded

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()
A = LI()
m = Counter(A).most_common()[0][0]
l = A.index(m)
r = n - A.index(m, l + 1)


n += 1
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):
    ret = comb(n, j)
    for k in range(j):
        ret = (ret - comb(l, k) * comb(r, j - k - 1)) % mod
    print(ret)

Submission status: Time Limit Exceeded

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()
A = LI()
m = Counter(A).most_common()[0][0]
l = A.index(m)
r = n - A.index(m, l + 1)


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


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


for j in range(1, n + 1):
    ret = comb(n, j)
    for k in range(j):
        ret -= comb(l, k) * comb(r, j - k - 1)
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
#D問題
import math
mod = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ind = [-1 for i in range(N)]
indset = []
for i in range(N+1):
    if ind[A[i]-1] == -1:
        ind[A[i]-1] = i
    else:
        indset = [ind[A[i]-1],i]

left = indset[0]
right = N-indset[1]
tan = left+right
#print(tan)
"""
fact = [1]
for i in range(N+1):
    fact.append(fact[-1]*(i+1))
"""

for i in range(N+1):
    if i == 0:
        print(N)
    else:
        I = i+1
        f1 = math.factorial(N+1) #fact[N+1]
        f2 = math.factorial(I) #fact[I]
        f3 = math.factorial(N+1-I) #fact[N+1-I]
        total = f1//(f2*f3) % mod
        if tan >= i:
            d1 = math.factorial(tan) #fact[tan]
            d2 = math.factorial(i) #fact[i]
            d3 = math.factorial(tan-i) #fact[tan-i]
            deg = d1//(d2*d3) % mod
        else:
            deg = 0
        #print(total,deg)
        print((total-deg)%mod)

Submission status: Time Limit Exceeded

Solution code:
#D問題
mod = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ind = [-1 for i in range(N)]
indset = []
for i in range(N+1):
    if ind[A[i]-1] == -1:
        ind[A[i]-1] = i
    else:
        indset = [ind[A[i]-1],i]

left = indset[0]
right = N-indset[1]
tan = left+right
#print(tan)
fact = [1]
for i in range(N+1):
    fact.append(fact[-1]*(i+1))

for i in range(N+1):
    if i == 0:
        print(N)
    else:
        I = i+1
        f1 = fact[N+1]
        f2 = fact[I]
        f3 = fact[N+1-I]
        total = f1//(f2*f3) % mod
        if tan >= i:
            d1 = fact[tan]
            d2 = fact[i]
            d3 = fact[tan-i]
            deg = d1//(d2*d3) % mod
        else:
            deg = 0
        #print(total,deg)
        print((total-deg)%mod)

Submission status: Time Limit Exceeded

Solution code:
#D問題
mod = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ind = [-1 for i in range(N)]
indset = []
for i in range(N+1):
    if ind[A[i]-1] == -1:
        ind[A[i]-1] = i
    else:
        indset = [ind[A[i]-1],i]

left = indset[0]
right = N-indset[1]
tan = left+right
#print(tan)
fact = [1]
for i in range(N+1):
    fact.append(fact[-1]*(i+1))

for i in range(N+1):
    if i == 0:
        print(N)
    else:
        I = i+1
        total = fact[N+1]//(fact[I]*fact[N+1-I])
        if tan >= i:
            deg = fact[tan]//(fact[i]*fact[tan-i])
        else:
            deg = 0
        #print(total,deg)
        print((total-deg)%mod)

Submission status: Time Limit Exceeded

Solution code:
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)//(i+1)
    if c >= i:
        print((comb-comb2)%mod)
        comb2 = comb2*(c-i)//(i+1)
    else:
        print(comb%mod)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import Counter
mod = 10**9 + 7

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

counter = Counter(li)
dup = counter.most_common()[0][0]
be, af = [i for i, x in enumerate(li) if x==dup]
soto = be + length - af - 1

for k in range(1, n+2):
    if k == 1:
        print(n)
    elif k == n+1:
        print(1)
    else:
        zyouhukuari = factorial(length) // (factorial(k)*factorial(length-k))
        if soto-k+1 >= 0:
            zyouhuku = factorial(soto) // (factorial(k-1)*factorial(soto-k+1))
        else:
            zyouhuku = 0
        val = zyouhukuari - zyouhuku
        print(val%mod)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import Counter
mod = 10**9 + 7

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

counter = Counter(li)
for k, v in counter.items():
    if v==2:
        dup = k
be, af = [i for i, x in enumerate(li) if x==dup]
soto = be + length - af - 1

for k in range(1, n+2):
    if k == 1:
        print(n)
    elif k == n+1:
        print(1)
    else:
        zyouhukuari = factorial(length) // (factorial(k)*factorial(length-k))
        if soto-k+1 >= 0:
            zyouhuku = factorial(soto) // (factorial(k-1)*factorial(soto-k+1))
        else:
            zyouhuku = 0
        val = zyouhukuari - zyouhuku
        print(val%mod)

Submission status: Time Limit Exceeded

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 = [n]
if n > 1:
    ans.append(comb(m1+m2+m3, 2) + m1+m2 + m2+m3 + 1)
for k in range(3, n+1):
    ansi = comb(n+1, k)
    for l in range(k):
        ansi -= comb(m1, l) * comb(m3, k-1-l)
        ansi = (ansi + MOD) % MOD

    ans.append(ansi)

ans.append(1)

for ansi in ans:
    print(ansi)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
n = int(input().strip())
A = list(map(int, input().strip().split(' ')))

dic_ = {}
for i,a in enumerate(A):
  if a in dic_:
    fix = dic_[a] + n-i  # 同じ数字2つの両方ともの前か後ろにある要素の数
    break
  else:
    dic_[a] = i

@lru_cache(maxsize=500)
def factorial(n):
  ans = 1
  for i in range(2,n+1):
    ans *= i
  return ans

@lru_cache(maxsize=500)
def C(n,r):
  return factorial(n)//(factorial(r)*factorial(n-r))

for k in range(1,n+2):
  if k == 1:
    print((n) % 1000000007)
  elif k-1 > fix :  # 重複が発生しない
    print(C(n+1,k) % 1000000007)
  else:
    print((C(n+1,k)-C(fix,k-1)) % 1000000007)

Submission status: Time Limit Exceeded

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

dic_ = {}
for i,a in enumerate(A):
  if a in dic_:
    fix = dic_[a] + n-i  # 同じ数字2つの両方ともの前か後ろにある要素の数
    break
  else:
    dic_[a] = i

def factorial(n):
  ans = 1
  for i in range(2,n+1):
    ans *= i
  return ans

def C(n,r):
  return factorial(n)//(factorial(r)*factorial(n-r))

for k in range(1,n+2):
  if k == 1:
    print((n) % 1000000007)
  elif k-1 > fix :  # 重複が発生しない
    print(C(n+1,k) % 1000000007)
  else:
    print((C(n+1,k)-C(fix,k-1)) % 1000000007)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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)
        # print((i * j) % div)
        pow2.append(pow2[-1]*j % div)

    # print(pow1)
    # print(pow2)
    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: Time Limit Exceeded

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)

    B = [1]
    v = 1
    for i in range(1, N+2):
        v = (v*(N-i+2))//i
        B.append(v % div)
    # print(B)

    C = [1]
    v = 1
    for i in range(1, M+1):
        v = (v*(M-i+1))//i
        C.append(v % div)

    for l in range(1, N+2):
        if l <= M+1:
            print((B[l] - C[l-1]) % div)
        else:
            print(B[l])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2) * power_func(c, mod - 2)) % 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)
    for i in range(1, n + 1):
        factorial[i] = (factorial[i - 1] * i) % 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: Time Limit Exceeded

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_mod1(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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2) * power_func(c, mod - 2)) % mod

    def combination_mod2(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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2) * power_func(c, mod - 2)) % 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)
    for i in range(1, n + 1):
        factorial[i] = (factorial[i - 1] * i) % mod
    for i in range(1, n + 1):
        ans = combination_mod1(n - 2, i)
        ans += combination_mod1(n - 2, i - 1) * 2
        ans += combination_mod1(n - 2, i - 2)
        ans -= combination_mod2(c + a, i - 1)
        ans -= combination_mod2(1, i)
        print(ans%mod)
    return

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

Submission status: Time Limit Exceeded

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, mod):
        """ a^b mod p を求める """
        """ bを2進数分解して高速累乗 """

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

    def combination_mod1(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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2, mod) * power_func(c, mod - 2, mod)) % mod

    def combination_mod2(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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2, mod) * power_func(c, mod - 2, mod)) % 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)
    for i in range(1, n + 1):
        factorial[i] = (factorial[i - 1] * i) % mod
    for i in range(1, n + 1):
        ans = combination_mod1(n - 2, i)
        ans += combination_mod1(n - 2, i - 1) * 2
        ans += combination_mod1(n - 2, i - 2)
        ans -= combination_mod2(c + a, i - 1)
        ans -= combination_mod2(1, i)
        print(ans%mod)
    return

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

Submission status: Time Limit Exceeded

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, mod):
        """ a^b mod p を求める """
        """ bを2進数分解して高速累乗 """

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

    def combination_mod1(n, k, mod):
        """ 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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2, mod) * power_func(c, mod - 2, mod)) % mod

    def combination_mod2(n, k, mod):
        """ 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 = factorial[k]
        c = factorial[n - k]
        return (a * power_func(b, mod - 2, mod) * power_func(c, mod - 2, mod)) % 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)
    for i in range(1, n + 1):
        factorial[i] = (factorial[i - 1] * i) % mod
    for i in range(1, n + 1):
        ans = combination_mod1(n - 2, i, mod)
        ans += combination_mod1(n - 2, i - 1, mod) * 2
        ans += combination_mod1(n - 2, i - 2, mod)
        ans -= combination_mod2(c + a, i - 1, mod)
        ans -= combination_mod2(1, i, mod)
        print(ans%mod)
    return

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