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:
n = int(input())
a = list(input().split())

if n == 1:
    print(1)
    print(1)
    exit()

MOD = 10 ** 9 + 7

b = sorted(a)

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

first = a.index(d)
for i in range(first + 1, n + 1):
    if a[i] == d:
        second = i

w = n + first - second

print(n)
x = n + 1
y = 1
z = 1
for _ in range(n):
    x *= n + 1 - z
    x //= z + 1
    if z <= w:
        y *= w + 1 - z
        y //= z
    else:
        y = 0
    print((x - y) % MOD)
    z += 1

Submission status: Time Limit Exceeded

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

if n == 1:
    print(1)
    print(1)
    exit()

MOD = 10 ** 9 + 7

b = sorted(a)

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

first = a.index(d)
for i in range(first + 1, n + 1):
    if a[i] == d:
        second = i

w = n + first - second

c = [0 for _ in range(n + 2)]
d = [0 for _ in range(n + 2)]

L = 1
H = 1
for i in range(1, n + 2):
    L *= i
    H *= n + 2 - i
    c[i] = L
    d[i] = H

e = [0 for _ in range(w + 1)]
temp = 1
for i in range(1, w + 1):
    temp *= w + 1 - i
    e[i] = temp


def ncmb(k):
    return d[k] // c[k]

def wcmb(k):
    if k > w or k <= 0:
        return 0
    elif k == w:
        return 1
    else:
        return e[k] // c[k]

print(n)
for k in range(2, n + 2):
    ans = ncmb(k) - wcmb(k-1)
    print(ans % MOD)

Submission status: Time Limit Exceeded

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

def comb(n,r):
    if n<r or r<0:
      return 0
    r = min(r,n-r)
    result = 1
    for i in range(n-r+1,n+1):
        result *= i
    for i in range(1,r+1):
        result //= i
    return result

sorted_a=sorted(a)
for i in range(n):
  if sorted_a[i]==sorted_a[i+1]:
    num = sorted_a[i]
    break

flag = True
for i in range(n+1):
  if a[i]==num:
    if flag:
      first = i
      flag = False
    else:
      second = i

combination=[0]*(n+2)
combination[0]=1
for i in range(1,n+2):
  combination[i] =combination[i-1]*(n+2-i)//i

combination2=[0]*(n+2)
combination2[0]=0
combination2[1]=1
for i in range(2,n+2):
  combination2[i] =combination2[i-1]*(first+n-second+1-(i-1))//(i-1)

  
for i in range(1,n+2):
  #ans = comb(n-1,i) + 2*comb(n-1,i-1)-comb(first+n-second,i-1) +comb(n-1,i-2)
  #ans = comb(n+1,i) - comb(first+n-second,i-1)
  ans = (combination[i] - combination2[i])%1000000007
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        #self.comb_N = {}
        self.power_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        #if (n, r) in self.comb_N:
        #    return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        if r not in self.power_N:
            self.power_N[r] = power_func(b,self.p-2,self.p)
        if n - r not in self.power_N:
            self.power_N[n - r] = power_func(c,self.p-2,self.p)
        return (a*self.power_N[r]*self.power_N[n - r])% self.p
        #self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        #return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1   
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        #A.append(int(input_raw[i]))
        A = int(input_raw[i])
        if A not in idx_dic:
            idx_dic[A] = i
        else:
            first_idx = idx_dic[A]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    sub_N = N - second_idx + first_idx
    for k in range(1, N + 1):
        #if k <= sub_N:
        print (cc.comb(N, k) -  cc.comb(sub_N - 1, k - 1)) %  1000000007  
        #else:
          #print (cc.comb(N, k)) + 1

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        #self.comb_N = {}
        self.power_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        #if (n, r) in self.comb_N:
        #    return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        if r not in self.power_N:
            self.power_N[r] = power_func(b,self.p-2,self.p)
        if n - r not in self.power_N:
            self.power_N[n - r] = power_func(c,self.p-2,self.p)
        return (a*self.power_N[r]*self.power_N[n - r])% self.p
        #self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        #return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1   
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        #A.append(int(input_raw[i]))
        A = int(input_raw[i])
        if A not in idx_dic:
            idx_dic[A] = i
        else:
            first_idx = idx_dic[A]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    sub_N = N - second_idx + first_idx
    for k in range(1, N + 1):
        if k <= sub_N:
          print (cc.comb(N, k) -  cc.comb(sub_N - 1, k - 1)) %  1000000007  
        else:
          print (cc.comb(N, k))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        #self.comb_N = {}
        self.power_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        #if (n, r) in self.comb_N:
        #    return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        if r not in self.power_N:
            self.power_N[r] = power_func(b,self.p-2,self.p)
        if n - r not in self.power_N:
            self.power_N[n - r] = power_func(c,self.p-2,self.p)
        return (a*self.power_N[r]*self.power_N[n - r])% self.p
        #self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        #return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = i
        else:
            first_idx = idx_dic[A[i]]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    sub_N = N - second_idx + first_idx
    for k in range(1, N + 1):
        if k <= sub_N:
          print (cc.comb(N, k) -  cc.comb(sub_N - 1, k - 1)) %  1000000007  
        else:
          print (cc.comb(N, k)) % 1000000007

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        #self.comb_N = {}
        self.power_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        #if (n, r) in self.comb_N:
        #    return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        if r not in self.power_N:
            self.power_N[r] = power_func(b,self.p-2,self.p)
        if n - r not in self.power_N:
            self.power_N[n - r] = power_func(c,self.p-2,self.p)
        return (a*self.power_N[r]*self.power_N[n - r])% self.p
        #self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        #return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = i
        else:
            first_idx = idx_dic[A[i]]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    res = [1]
    sub_N = N - second_idx + first_idx
    #Np1Ck = cc.comb(N+1, N+1)
    #NCK
    for k in range(1, N + 1):
        sub1 = 0
        if k <= sub_N:
            sub1 = cc.comb(sub_N - 1, k - 1)  
        print (cc.comb(N, k) - sub1) % 1000000007

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        self.comb_N = {}
        self.power_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        if (n, r) in self.comb_N:
            return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        if r not in self.power_N:
            self.power_N[r] = power_func(b,self.p-2,self.p)
        if n - r not in self.power_N:
            self.power_N[n - r] = power_func(c,self.p-2,self.p)
        self.comb_N[(n, r)] = (a*self.power_N[r]*self.power_N[n - r])% self.p
        self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = i
        else:
            first_idx = idx_dic[A[i]]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    res = [1]
    sub_N = N - second_idx + first_idx
    #Np1Ck = cc.comb(N+1, N+1)
    #NCK
    for k in range(1, N + 1):
        sub1 = 0
        if k <= sub_N:
            sub1 = cc.comb(sub_N - 1, k - 1)  
        print (cc.comb(N, k) - sub1) % 1000000007

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        self.comb_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        if (n, r) in self.comb_N:
            return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        self.comb_N[(n, r)] = (a*power_func(b,self.p-2,self.p)*power_func(c,self.p-2, self.p))% self.p
        self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = i
        else:
            first_idx = idx_dic[A[i]]
            second_idx = i
            break

    cc = comb_calclator(N, 1000000007)
    res = [1]
    sub_N = N - second_idx + first_idx
    #Np1Ck = cc.comb(N+1, N+1)
    #NCK
    for k in range(1, N + 1):
        sub1 = 0
        if k <= sub_N:
            sub1 = cc.comb(sub_N - 1, k - 1)  
        print (cc.comb(N, k) - sub1) % 1000000007

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        self.comb_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        if (n, r) in self.comb_N:
            return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        self.comb_N[(n, r)] = (a*power_func(b,self.p-2,self.p)*power_func(c,self.p-2, self.p))% self.p
        self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = [i]
        else:
            idx_dic[A[i]].append(i)
    for i in range(N):
        if len(idx_dic[A[i]]) >= 2:
            first_idx = min(idx_dic[A[i]])
            second_idx = max(idx_dic[A[i]])
            break

    cc = comb_calclator(N, 1000000007)
    res = [1]
    sub_N = N - second_idx + first_idx
    #Np1Ck = cc.comb(N+1, N+1)
    #NCK
    for k in range(1, N + 1):
        sub1 = 0
        if k <= sub_N:
            sub1 = cc.comb(sub_N - 1, k - 1)  #cc.comb(sub_N, k) - cc.comb(sub_N - 1, k)
##        if k <= N - second_idx:
##            sub1 = cc.comb(N - second_idx, k) - cc.comb(N - second_idx - 1, k)
##        if  k <= first_idx + 1:
##            sub2 = cc.comb(first_idx + 1, k) - cc.comb(first_idx + 1 - 1, k)
        print (cc.comb(N, k) - sub1) % 1000000007

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

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

class comb_calclator():
    def __init__(self, N, p):
        self.N = N
        self.p = p
        self.frac_N = [0 for i in range(N + 1)]
        self.frac_N[0] = 1L
        self.comb_N = {}
        for i in range(1, N + 1):
            self.frac_N[i] = (self.frac_N[i - 1] * i ) % p

    def comb(self, n, r):
        if n<0 or r<0 or n<r: return 0
        if n==0 or r==0: return 1
        if (n, r) in self.comb_N:
            return self.comb_N[(n, r)]
        a = self.frac_N[n]
        b = self.frac_N[r]
        c = self.frac_N[n - r]
        self.comb_N[(n, r)] = (a*power_func(b,self.p-2,self.p)*power_func(c,self.p-2, self.p))% self.p
        self.comb_N[(n, n - r)] = self.comb_N[(n, r)]
        return self.comb_N[(n, r)]

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    N = n + 1
    for i in range(N):
        A.append(int(input_raw[i]))
    idx_dic = {}
    second_idx = 0
    first_idx = 0
    twice_a = 0
    for i in range(N):
        if A[i] not in idx_dic:
            idx_dic[A[i]] = [i]
        else:
            idx_dic[A[i]].append(i)
    for i in range(N):
        if len(idx_dic[A[i]]) >= 2:
            first_idx = min(idx_dic[A[i]])
            second_idx = max(idx_dic[A[i]])
            break

    cc = comb_calclator(N, 1000000007)
    for k in range(1, N+1):
        sub1 = 0
        sub2 = 0
        sub_N = N - second_idx + first_idx
        if k <= sub_N:
            sub1 = cc.comb(sub_N-1, k-1)#cc.comb(sub_N, k) - cc.comb(sub_N - 1, k)
##        if k <= N - second_idx:
##            sub1 = cc.comb(N - second_idx, k) - cc.comb(N - second_idx - 1, k)
##        if  k <= first_idx + 1:
##            sub2 = cc.comb(first_idx + 1, k) - cc.comb(first_idx + 1 - 1, k)
        print (cc.comb(N, k) - sub1 ) % 1000000007




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
mod = 10**9+7

memo=[0]*(N+2)
def comb(x, y):
    if x == N-1 and memo[y] > 0:
        return memo[y]
    elif y > x:
        return 0
    elif y * 2 > x:
        y = x - y
    result = 1
    for j in range(1, y + 1):
        result *= (x + j - y) * pow(j, mod - 2, mod)
        result %= mod
    if x == N-1:
        memo[y] = result
    return result


dupl = sum(A)-N*(N+1)//2
ds = []
for _ in range(N+1):
    if A[_] == dupl:
        ds += [_]
i, j = ds

print(N)
for k in range(2, N+2):
    print((comb(N-1, k)+comb(N-1, k-2)+2*comb(N-1, k-1)-comb(N-j+i, k-1)) % mod)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
mod = 10**9+7


def comb(x, y):
    if y > x:
        return 0
    elif y * 2 > x:
        y = x - y
    result = 1
    for j in range(1, y + 1):
        result *= (x + j - y) * pow(j, mod - 2, mod)
        result %= mod
    return result


dupl = sum(A)-N*(N+1)//2
ds = []
for _ in range(N+1):
    if A[_] == dupl:
        ds += [_]
i, j = ds

print(N)
for k in range(2, N+2):
    print((comb(N-1, k)+comb(N-1, k-2)+2*comb(N-1, k-1)-comb(N-j+i, k-1)) % mod)

Submission status: Time Limit Exceeded

Solution code:
#import itertools
import math
n = int(input())
a = input().split()
a = [int(m) for m in a]
dic = {}
dic2 = {}
for i in range(n + 1):
    dic[a[i]] = 0
    dic2[a[i]] = []
k = 0
j = 0
for i in range(n + 1):
    dic[a[i]] += 1
    dic2[a[i]].append(i)
    if dic[a[i]] == 2:
        j = dic2[a[i]][0]
        k = i

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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 = n + 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 )

#a = cmb(10**9,80,mod)

for i in range(n + 1):
    if i == 0:
        print(n)
    else:
        if j + n - k >= i:
            print((combinations_count(n + 1, i + 1) - combinations_count(j + n - k, i)) % mod)
        else:
            print(cmb(n + 1, i + 1, mod))

Submission status: Time Limit Exceeded

Solution code:
#import itertools
import math
n = int(input())
a = input().split()
a = [int(m) for m in a]
mod = 10**9 + 7
dic = {}
dic2 = {}
for i in range(n + 1):
    dic[a[i]] = 0
    dic2[a[i]] = []
k = 0
j = 0
for i in range(n + 1):
    dic[a[i]] += 1
    dic2[a[i]].append(i)
    if dic[a[i]] == 2:
        j = dic2[a[i]][0]
        k = i

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(n + 1):
    if i == 0:
        print(n)
    else:
        if j + n - k >= i:
            print((combinations_count(n + 1, i + 1) - combinations_count(j + n - k, i)) % mod)
        else:
            print(combinations_count(n + 1, i + 1) % mod)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)

# ax ≡ 1 (mod m)
def modinv(a, m):
    (inv, q, gcd_val) = egcd(a, m)
    return inv % m



def modfact(n,N):
    r = 1
    for i in range(1,n+1):
        r = r % N * i % N
    return r


def modconb(n,r,N):
    p = modfact(n,N)
    q1 = modinv(modfact(r,N),N)
    q2 = modinv(modfact(n-r,N),N)
    return p*q1%N*q2%N

N = 10**9+7
n = int(input())
a = list(map(int,input().split()))
b=[0]*n
for i in range(n+1):
    b[a[i]-1] += 1
w = b.index(2)+1
index = []
for i in range(n+1):
    if a[i] == w:
        index.append(i)
l = index[0]
r = n - index[1]

for i in range(n+1):
    ans = modconb(n+1,i+1,N)
    if l+r >= i:
        ans -= modconb(l+r,i,N)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
mod = 7 + 10 ** 9

def Pow(i, n):
    if n == 0:
        return 1
    else:
        ans = Pow(i * i % mod, n//2)
        if n % 2 == 1:
            ans *= i
            ans %= mod
        return ans

Nfact = [1]
NfactRev = []
for i in range(1, N+2):
    Nfact.append((Nfact[i-1] * (i)) % mod)
    if A.count(i) == 2:
        Double = [A.index(i), A[::-1].index(i)]
NfactRev.append(Pow(Nfact[N+1], mod-2))
for i in range(1, N+1):
    NfactRev.append(NfactRev[i-1] * (N+2-i) % mod)
NfactRev.append(1)

doublearray = Double[0] + Double[1]
for i in range(1, N+2):
    whole = Nfact[N+1] * NfactRev[-(N+2-i)] * NfactRev[-(i+1)] % mod
    if i <= doublearray+1:
        double = Nfact[doublearray] * NfactRev[-(doublearray-i+2)] * NfactRev[-i] % mod
    else:
        double = 0
    print(whole - double)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
mod = 7 + 10 ** 9

def Pow(i, n):
    if n == 0:
        return 1
    else:
        ans = Pow(i * i % mod, n//2)
        if n % 2 == 1:
            ans *= i
            ans %= mod
        return ans

Nfact = [1]
NfactRev = []
for i in range(1, N+2):
    Nfact.append((Nfact[i-1] * (i)) % mod)
    if A.count(i) == 2:
        Double = [j for j, x in enumerate(A) if x == i]
NfactRev.append(Pow(Nfact[N+1], mod-2))
for i in range(1, N+1):
    NfactRev.append(NfactRev[i-1] * (N+2-i) % mod)
NfactRev.append(1)

doublearray = N - (Double[1] - Double[0])
for i in range(1, N+2):
    whole = Nfact[N+1] * NfactRev[-(N+2-i)] * NfactRev[-(i+1)] % mod
    if i <= doublearray+1:
        double = Nfact[doublearray] * NfactRev[-(doublearray-i+2)] * NfactRev[-i] % mod
    else:
        double = 0
    print(whole - double)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
mod = 7 + 10 ** 9

def Pow(i, n):
    if n == 0:
        return 1
    else:
        ans = Pow(i * i % mod, n//2)
        if n % 2 == 1:
            ans *= i
            ans %= mod
        return ans

Nfact = [1]
NfactRev = [1]
for i in range(1, N+2):
    Nfact.append((Nfact[i-1] * (i)) % mod)
    NfactRev.append((NfactRev[i-1] * Pow(i, mod - 2)) % mod)
    if A.count(i) == 2:
        Double = [j for j, x in enumerate(A) if x == i]

doublearray = N - (Double[1] - Double[0])
for i in range(1, N+2):
    whole = Nfact[N+1] * NfactRev[N+1-i] * NfactRev[i] % mod
    if i <= doublearray+1:
        double = Nfact[doublearray] * NfactRev[doublearray-i+1] * NfactRev[i-1] % mod
    else:
        double = 0
    print(whole - double)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


# nの逆元のリスト
def inv_mod(n:int, mod:int) -> list:
    inv = [0,1]
    for i in range(2,n+1):
        inv.append(mod - ((mod//i)*inv[mod%i]) % mod)
    return inv
       
# nの階乗のリスト
def fact(n:int, mod:int) -> list:
    fac = [1,1]
    res = 1
    for i in range(2,n+1):
        res = res*i%mod
        fac.append(res)
    return fac

# nの階乗の逆元のリスト
def fact_inv(n:int, inv:list, mod:int) -> list:
    facInv = [1,1]
    for i in range(2,n+1):
        facInv.append(facInv[i-1]*inv[i] % mod)
    return facInv
        
# 二項係数
def nCr(n:int, r:int, mod:int, fac:list, facInv:list) -> int:
    if not (0<=r and r<=n):
        return 0
    
    return ((fac[n]*facInv[r]) % mod) * facInv[n-r] % mod


from collections import Counter

MOD = 10**9+7

n = ni()
a = list(li())

# 被っている数の後にある数の個数を把握
cnt = Counter(a)
db = 0
for k,v in cnt.items():
    if v == 2:
        db = k
        
front = a.index(db)
back = a[::-1].index(db)

# 二項係数の準備
inv = inv_mod(n+1,MOD)
fac = fact(n+1, MOD)
facInv = fact_inv(n+1,inv,MOD)

# 出力
for k in range(1, n+2):
    ans = nCr(n+1,k,MOD,fac,facInv)
    for i in range(k):
        temp = nCr(front,i,MOD,fac,facInv) * nCr(back,k-1-i,MOD,fac,facInv)
        temp %= MOD
        ans -= temp
        if ans<0:
            ans += MOD
        
    print(ans)