Problem:


Score : 800 points

Problem Statement

There are N squares lining up in a row, numbered 1 through N from left to right.
Initially, all squares are white.
We also have N-1 painting machines, numbered 1 through N-1.
When operated, Machine i paints Square i and i+1 black.

Snuke will operate these machines one by one.
The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i.

Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P.
Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations.
Find the sum of the scores over all possible permutations for him.
Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

2 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of the scores over all possible permutations, modulo 10^9+7.

Sample Input 1

4

Sample Output 1

16

There are six possible permutations as P.
Among them, P = (1, 3, 2) and P = (3, 1, 2) have a score of 2, and the others have a score of 3.
Thus, the answer is 2 \times 2 + 3 \times 4 = 16.

Sample Input 2

2

Sample Output 2

1

There is only one possible permutation: P = (1), which has a score of 1.

Sample Input 3

5

Sample Output 3

84

Sample Input 4

100000

Sample Output 4

341429644



Sample input:
4


Sample output:
16

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
mod = 10**9+7

if N == 2:
    print(1)
    exit()
elif N == 3:
    print(4)
    exit()

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

kaijou_inverse = [1]*(N+1)
for i in range(1,N+1):
    kaijou_inverse[i] = pow(kaijou[i],mod-2,mod)

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

ans = 0
for i in range(1,N):
    ans += i*kaijou[i]*kaijou[N-i-i]*nCr(i-1,N-1-i)
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
mod=10**9+7
fra=[1]*(n+2)
inv=[1]*(n+2)
t1=1
t2=1
for i in range(1,n+2):
    t1*=i
    t1%=mod
    t2*=pow(i,mod-2,mod)
    t2%=mod
    fra[i]=t1
    inv[i]=t2
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
ans=0
t=0
for i in range((n+1)//2,n):
    a=comb(i-1,n-1-i)*fra[i]%mod
    ans+=(a-t)*fra[n-1-i]*i
    ans%=mod
    t+=a
    t%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
if N==2:
    print(1)
    sys.exit()
if N==3:
    print(2)
    sys.exit()
mod=10**9+7
table=[1]*(N+3)
t=1
for i in range(1,N+3):
    t*=i
    t%=mod
    table[i]=t
rtable=[1]*(N+3)
t=1
for i in range(1,N+3):
    t*=pow(i,mod-2,mod)
    t%=mod
    rtable[i]=t
#print(table,rtable)
ans=0
l=0
for k in range(N):
    if 0>2*k-N:
        continue
    num=table[k]*table[N-1-k]
    div=table[k-1]*rtable[N-1-k]*rtable[k-1-(N-1-k)]
    div%=mod
    num*=div
    num%=mod
    ans+=k*(num-l)
    l=num
    l%=mod
    ans%=mod
    #print(k, num, div)
print(ans)

Submission status: Wrong Answer

Solution code:
Md,MN=1000000007,1000006
fac,ifac,a,res,inv=[1,1],[1,1],[0],0,[1,1]
for i in range(2,MN):
    inv.append(inv[Md%i]*(Md-Md/i)%Md)
    fac.append(fac[-1]*i%Md)
    ifac.append(ifac[-1]*inv[i]%Md)
def C(x,y):
    if(x<y):
        return 0
    return fac[x]*ifac[y]*ifac[x-y]
def D(x,y):
    return C(x-y+1,y)

n=int(input())
for i in range(1,n):
    if i*2-n>=0:
        a.append(D(n-3,n-i-1)*fac[i]*fac[n-i-1]%Md)
    else:
        a.append(0)
for i in range(n-1,0,-1):
    a[i]=(a[i]-a[i-1])%Md
    res=res+a[i]*i
res=res%Md
print(res)

Submission status: Wrong Answer

Solution code:

N = int(input())

F = [1]
#F_ = [1]
n = 1
for i in range(1, N):
    n *= i
    n %= 1e9+7
    F.append(n)
    #F_.append(1/n)
#print(F)

C = lambda n,r: (F[n]/(F[r]*F[n-r]))%(1e9+7)

ans = 0
cnt = 0
for i in range((N+1)//2, N):
    #print(i, N-i-1,C(i-1, N-i-1),F[i],F[N-i-1])
    tmp = C(i-1, N-i-1)*F[i]*F[N-i-1]
    ans += (tmp-cnt)%(1e9+7)*i
    cnt = tmp
print(int(ans%(1e9+7)))

Submission status: Wrong Answer

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_

def invfact(n, f, p):
    m = pow(f[n], p-2, p)
    yield m
    for i in range(n, 0, -1):
        m = m * i % p
        yield m

ans = 0
m = n - 1
f = list(fact(m))
rf = list(invfact(m, f, p))
rf.reverse()
perm = 0

for k in range((n+1)//2, n):
    perm_ = f[k-1] * rf[2*k-n-2] %p * f[k] % p
    ans += (perm_ - perm) %p * k % p
    ans %= p
    perm = perm_

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_

ans = 0
m = n - 1
f = list(fact(m))
perm = 0

for k in range((n+1)//2, n):
    b = m - k
    a = (m - 1) - (2 * b)
    perm_ = f[a+b] * pow(f[a], p-2, p) %p * pow(f[b], p-2, p) %p * f[k] % p * f[m-k] % p
    ans += (perm_ - perm) %p * k % p
    ans %= p
    perm = perm + perm_ %p

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_

ans = 0
m = n - 1
f = list(fact(m))
perm = 0

for k in range((n+1)//2, n):
    b = m - k
    a = (m - 1) - (2 * b)
    perm_ = f[a+b] * pow(f[a], p-2, p) %p * pow(f[b], p-2, p) %p * f[k] % p * f[m-k] % p
    ans += (perm_ - perm) %p * k % p
    perm = perm + perm_ %p

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_


ans = 0
m = n - 1
f = list(fact(m))
perm = 0

for k in range(n//2, n):
    b = m - k
    a = (m - 1) - (2 * b)
    perm_ = f[a+b]//(f[a]*f[b]) %p * f[k] % p * f[m-k] % p
    ans += (perm_ - perm) * k % p
    perm += perm_

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    for i in range(n-1, 0, -1):
         n_ = (n_*i) % p
    return n * n_ % p, n_

def nfact(n):
    for i in range(n-1, 0, -1):
         n = (n*i) % p
    return n

def comb(n, r):
    return fact(n)//(fact(r) * fact(n-r))

ans = 0
m = n - 1
if n == 2:
    ans = 1

elif m % 2 == 0:
    fm, fm_ = fact(m)
    #mc = fact(m-1)
    #ans += mc * 2 *m
    ans += fm * 2 % p
    fm -= fm_ * 2
    ans += fm * (m-1) % p
    
else:
    fm, fm_ = fact(m)
    #mc = fact(m-1)
    #ans += mc * 2 *m
    hn = n // 2
    fhn, fhn_ = fact(hn)
    ans += fhn * fhn_ % p * (hn) % p
    ans += fm_ * 2 % p * m % p
    fm -= fhn * fhn_
    fm -= fm_ * 2
    ans += fm * (m-1)
    # ans += fhn * hn % p
    # ans += fhn * (((hn+1) // 2 + hn) * hn % p) % p
    # fm -= fhn * 2
    # ans += fm * m % p

print(ans)

Submission status: Wrong Answer

Solution code:
law = 10**9+7
N = long(raw_input())
ans = 0L
if N == 2:
  ans = 1
elif N ==3:
  ans = 4
else:
  factorio = [1L]
  t = 1L
  for i in range(N+1)[1:]:
    t *= i
    t %= law
    factorio.append(t)
  num = 0
  past_ruiseki = 0
  for K in range(N/2, N):
    ruiseki = (((factorio[K]*factorio[K-1])%law)/factorio[2*K-N])%law
    num = (ruiseki-past_ruiseki)%law
    ans += (K*num)%law
    past_ruiseki = ruiseki
print ans

Submission status: Wrong Answer

Solution code:
law = 10**9+7
N = long(raw_input())
ans = 0L
if N == 2:
  ans = 1
elif N ==3:
  ans = 4
else:
  factorio = [1L]
  t = 1L
  for i in range(N+1)[1:]:
    t *= i
    t %= law
    factorio.append(t)
  num = 0
  past_ruiseki = 0
  for K in range(N/2, N):
    ruiseki = (((factorio[K]*factorio[K-1])%law)/factorio[2*K-N])%law
    num = (ruiseki-past_ruiseki)%law
    ans += K*num
    past_ruiseki = ruiseki
print ans

Submission status: Wrong Answer

Solution code:
# seishin.py
N = int(input())
MOD = 10**9 + 7

v = 1
for K in range(2, (N+1)//2):
    v = v * K * (K-1) % MOD

b = 1
ans = cnt = 0
for K in range((N+1)//2, N):
    v = v * K * (K-1) % MOD
    ans += (v - cnt) * K % MOD
    cnt = v
    ans %= MOD
    v = v * pow((2*K-N+1)*(2*K-N+2), MOD-2, MOD) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    binarylist=list(str(bin(input2))[2:])[::-1]
    binarysize=len(binarylist)
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
Finv=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(Fact[i-1],i)
    Finv[i]=divide(1,Fact[i])
ans=0
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    SGL[K]=times(times(Fact[K],Finv[2*K-N]),Fact[K-1])
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Wrong Answer

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    binaryl=list(str(bin(input2))[2:])
    binarysize=len(binaryl)
    binarylist=[int(binaryl[binarysize-1-i]) for i in range(binarysize)]
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(i,Fact[i-1])
ans=0
def C(A,B):
    return divide(Fact[A],times(Fact[B],Fact[A-B]))
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    F=times(Fact[K],Fact[N-1-K])
    G=C(K-1,N-1-K)
    SGL[K]=(times(F,G))
for K in range(1,N):
    ans+=times(SGL[K]-SGL[K-1],K)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

MOD = 10**9 + 7

N = read()

NN = N + 1
fact = [1] * NN
for i in range(1, NN):
  fact[i] = (fact[i-1] * i) % MOD

def inv(n):
  return pow(n, MOD-2, MOD)

def comb(n, k):
  return fact[n] * inv(fact[n-k]) % MOD * inv(fact[k]) % MOD

S = [0] * N
for k in range((N+1) // 2, N):
  c = comb(k-1, N-1-k)
  S[k] = c * fact[k] % MOD

# print(S, file=stderr)

ans = 0
for k in range((N+1) // 2, N):
  ans = (ans + k * (S[k] - S[k-1] * (N - k)) % MOD) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
def permutations(L):
    if L == []:
        return [[]]
    else:
        return [[h]+t for i,h in enumerate(L)
                      for t   in permutations(L[:i]+L[i+1:])]

n = int(input())

perm_list = []
for i in range(n - 1):
    perm_list.append(str(i+1))
perm_result = permutations(perm_list)
#print(perm_result)
score = 0
for p in perm_result:
    m = [0] * n
    for j in range(len(p)):
        m[int(p[j])-1] = 1
        m[int(p[j])] = 1
        mini_score = 0
        for i in range(n-1):
            mini_score += m[i]
        if mini_score == n-1:
            score += j+1
            break

print(score)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
nn = [i for i in range(0, n - 1)]
ans = 0
for element in itertools.permutations(nn, n - 1):
    field = 0
    tm = 0
    for elm in element:
        blk = (1 << elm) + (1 << (elm + 1))
        field = field | blk
        tm += 1
        if field == (1 << n) - 1:
            break
    ans += tm
    ans = ans % (10^9 + 7)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import collections
n = int(input())
machine = []
[machine.append(i) for i in range(n-1)]
result = []
for i in permutations(machine):
    l = []
    [l.append(i) for i in range(n)]
    score = 0
    for j in i:
        l[j] = -1
        l[j + 1] = -1
        score += 1
        if(all(x == -1 for x in l)):
            result.append(score)
            break
print(result)
temp = collections.Counter(result)
ans = 0
for k,v in temp.items():
    ans += k*v
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

ANS=0
MINUS=0

def k_perm(k):
    return Combi(k-1,N-k-1)*FACT[k]*FACT[N-k-1]%mod

for k in range(N):
    ANS=(ANS+(k_perm(k)-k_perm(k-1))*k)%mod

print(ANS)

Submission status: Time Limit Exceeded

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    return pow(input1,input2,characteristic)
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
Finv=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(Fact[i-1],i)
    Finv[i]=divide(1,Fact[i])
ans=0
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    SGL[K]=times(times(Fact[K],Finv[2*K-N]),Fact[K-1])
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
'''
def exponen(input1,input2):
    binaryl=list(str(bin(input2))[2:])
    binarysize=len(binaryl)
    binarylist=[int(binaryl[binarysize-1-i]) for i in range(binarysize)]
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
'''
def exponen(input1,input2):
    if input2==0:
        return 1
    elif input2%2==0:
        return exponen(times(input1,input1),input2//2)
    else:
        return times(input1,exponen(times(input1,input1),input2//2))

def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
Finv=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(Fact[i-1],i)
    Finv[i]=divide(1,Fact[i])
ans=0
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    SGL[K]=times(times(Fact[K],Finv[2*K-N]),Fact[K-1])
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
Md,MN=1000000007,1000006
fac,ifac,a,res=[1],[1],[0],0
for i in range(1,MN):
    fac.append(fac[-1]*i%Md)
    ifac.append(pow(fac[-1],Md-2,Md))
def C(x,y):
    if(x<y):
        return 0
    return fac[x]*ifac[y]*ifac[x-y]
def D(x,y):
    return C(x-y+1,y)

n=int(input())
for i in range(1,n):
    if i*2-n>=0:
        a.append(D(n-3,n-i-1)*fac[i]*fac[n-i-1]%Md)
    else:
        a.append(0)
for i in range(n-1,0,-1):
    a[i]=(a[i]-a[i-1])%Md
    res=res+a[i]*i
res=res%Md
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

MOD = 10**9+7
fac = [1,1] + [0]*N
finv = [1,1] + [0]*N
inv = [0,1] + [0]*N
for i in range(2,N+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

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

ans = 0
prev = 0
for k in range((N+1)//2, N):
    tmp = ncr(k-1, N-1-k) * fac[k] * fac[N-1-k]
    tmp %= MOD
    ans += (tmp - prev) * k
    ans %= MOD
    prev = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

N = int(input())

mod = 1000000007

def euclid(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)

_modinv = {}
def modinv(a):
    if a not in _modinv:
        inv, _, _ = euclid(a, mod)
        _modinv[a] = inv%mod
    return _modinv[a]

_factorial = {0: 1}
for n in range(1, N+1):
    _factorial[n] = n*_factorial[n-1]%mod

def factorial(n):
    return _factorial[n]

def comb(n, k):
    return factorial(n)*modinv(factorial(k))*modinv(factorial(n-k))%mod

if N == 2:
    print(1)
    exit(0)
if N == 3:
    print(4)
    exit(0)

res = 0
mem = 0
    
for K in range((N+1)//2, N):
    tmp = factorial(K)*factorial(N-1-K)*comb(K-1, N-K-1)
    just = (tmp-mem)%mod
    mem = tmp
    res += K*just
    res %= mod
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
import itertools
a=list(range(1,int(input())))
b=[]
for i in itertools.permutations(a):
    for I in range(len(i)):
        if len(set(n+z for z in [0,1]for n in i[0:I+1]))>max(a):
            b+=[I+1];break
print(sum(b))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_

def invfact(n, f, p):
    for i in range(n):
        yield pow(f[i], p-2, p)

ans = 0
m = n - 1
f = list(fact(m))
rf = list(invfact(n, f, p))
perm = 0

for k in range((n+1)//2, n):
    b = m - k
    a = (m - 1) - (2 * b)
    perm_ = f[a+b] * rf[a] %p * f[k] % p
    ans += (perm_ - perm) %p * k % p
    ans %= p
    perm = perm_

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
p = 10**9 + 7

def fact(n):
    n_ = 1
    yield n_
    for i in range(1, n+1):
         n_ = (n_*i) % p
         yield n_

ans = 0
m = n - 1
f = list(fact(m))
perm = 0

for k in range((n+1)//2, n):
    b = m - k
    a = (m - 1) - (2 * b)
    perm_ = f[a+b] * pow(f[a], p-2, p) %p * f[k] % p
    ans += (perm_ - perm) %p * k % p
    ans %= p
    perm = perm_

print(ans)

Submission status: Time Limit Exceeded

Solution code:
P=10**9+7
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)
def inv(x):
    return egcd(x,P)[0]
N=int(input())
Fact=[0 for i in range(N+1)]
Finv=[0 for i in range(N+1)]
Fact[0]=1
Finv[0]=1
for i in range(N):
    Fact[i+1]=((i+1)*Fact[i])%P
    Finv[i+1]=(Finv[i]*inv(i+1))%P
SGN=[0 for i in range(N)]
ans=0
for k in range(N):
    if 2*k-N>=0:
        SGN[k]=(((Fact[k-1]*Fact[k])%P)*Finv[2*k-N])%P
        ans=(ans+k*(SGN[k]-SGN[k-1]))%P
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# seishin.py
N = int(input())
MOD = 10**9 + 7

fact = [1]*(N+1)

for i in range(1, N+1):
    fact[i] = r = i*fact[i-1] % MOD

ans = cnt = 0
for K in range((N+1)//2, N):
    res = fact[K]*fact[K-1]*pow(fact[2*K-N], MOD-2, MOD) % MOD
    ans += (res - cnt) * K % MOD
    cnt = res
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# seishin.py
N = int(input())
MOD = 10**9 + 7

fact = [1]*(N+1)
rfact = [1]*(N+1)

for i in range(1, N+1):
    fact[i] = r = i*fact[i-1] % MOD
    rfact[i] = pow(r, MOD-2, MOD)

ans = 0
cnt = 0
for K in range(1, N):
    if not K >= N-K >= 1:
        continue
    res = fact[K]*fact[K-1]*rfact[2*K-N] % MOD
    ans += (res - cnt) * K % MOD
    cnt = res
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# seishin.py
N = int(input())
MOD = 10**9 + 7

fact = [1]*(N+1)
rfact = [1]*(N+1)

for i in range(1, N+1):
    fact[i] = r = i*fact[i-1] % MOD
    rfact[i] = pow(r, MOD-2, MOD)

ans = 0
cnt = 0
for K in range(1, N):
    if not K >= N-K >= 1:
        continue
    res = fact[K]*fact[K-1]*rfact[2*K-N] % MOD
    ans += (res - cnt) * K % MOD
    cnt = res
print(ans % MOD)

Submission status: Time Limit Exceeded

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    binaryl=list(str(bin(input2))[2:])
    binarysize=len(binaryl)
    binarylist=[int(binaryl[binarysize-1-i]) for i in range(binarysize)]
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
Finv=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(Fact[i-1],i)
    Finv[i]=divide(1,Fact[i])
ans=0
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    SGL[K]=times(times(Fact[K],Finv[2*K-N]),Fact[K-1])
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) - 1

LARGE = 10**9+7

def ex_euclid(x, y):
    c0, c1 = x, y
    a0, a1 = 1, 0
    b0, b1 = 0, 1
 
    while c1 != 0:
        m = c0 % c1
        q = c0 // c1
 
        c0, c1 = c1, m
        a0, a1 = a1, (a0 - q * a1)
        b0, b1 = b1, (b0 - q * b1)
 
    return c0, a0, b0
# precompute
fac_list = [1]*(N+1)
fac = 1
for i in range(1, N+1):
    fac = (fac * i) % LARGE
    fac_list[i] = fac
    
fac_inv_list = [1]*(N+1)
for i in range(N+1):
    fac_inv_list[i] =  pow(fac_list[i], LARGE-2, LARGE)
    
def nCr(n, r):
    return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE

def pat(n, r):
    return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE


pat = 0
score = 0

for k in range(N+1):
    if k-1 >= N-k:
        res = (((fac_list[k-1]*fac_list[k]) % LARGE) * fac_inv_list[k-1-N+k]) % LARGE
        score = (score + (res - pat) * k) % LARGE
        # print(k, pat, res)
        pat = res
print(score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) - 1

LARGE = 10**9+7

def ex_euclid(x, y):
    c0, c1 = x, y
    a0, a1 = 1, 0
    b0, b1 = 0, 1
 
    while c1 != 0:
        m = c0 % c1
        q = c0 // c1
 
        c0, c1 = c1, m
        a0, a1 = a1, (a0 - q * a1)
        b0, b1 = b1, (b0 - q * b1)
 
    return c0, a0, b0
# precompute
fac_list = [1]*(N+1)
fac = 1
for i in range(1, N+1):
    fac = (fac * i) % LARGE
    fac_list[i] = fac
    
fac_inv_list = [1]*(N+1)
for i in range(N+1):
    c,a,b = ex_euclid(fac_list[i], LARGE)
    fac_inv_list[i] = a % LARGE
    
def nCr(n, r):
    return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE

def pat(n, r):
    return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE


pat = 0
score = 0

for k in range(N+1):
    if k-1 >= N-k:
        res = (((nCr(k-1, N-k)*fac_list[k]) % LARGE) * fac_list[N-k]) % LARGE
        score = (score + (res - pat) * k) % LARGE
        # print(k, pat, res)
        pat = res
print(score)

Submission status: Time Limit Exceeded

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    binaryl=list(str(bin(input2))[2:])
    binarysize=len(binaryl)
    binarylist=[int(binaryl[binarysize-1-i]) for i in range(binarysize)]
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
Finv=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(Fact[i-1],i)
    Finv[i]=divide(Finv[i-1],i)
ans=0
def C(A,B):
    return times(Fact[A],times(Finv[B],Finv[A-B]))
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    SGL[K]=times(times(Fact[K],Fact[K-1]),Finv[2*K-N])
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

MOD = 10**9 + 7

N = read()

NN = N + 1
fact = [1] * NN
for i in range(1, NN):
  fact[i] = (fact[i-1] * i) % MOD

def inv(n):
  return pow(n, MOD-2, MOD)

invfact = [inv(fact[i]) for i in range(NN)]

def comb(n, k):
  return fact[n] * invfact[n-k] % MOD * invfact[k] % MOD

S = [0] * N
for k in range((N+1) // 2, N):
  S[k] = comb(k-1, N-1-k) * fact[k] % MOD * fact[N-1-k] % MOD

ans = 0
for k in range((N+1) // 2, N):
  ans = (ans + k * (S[k] - S[k-1]) % MOD) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
characteristic=10**9+7
def plus(input1,input2):
    return (input1+input2)%characteristic
def minus(input1,input2):
    return (input1-input2)%characteristic
def times(input1,input2):
    return (input1*input2)%characteristic
def exponen(input1,input2):
    binaryl=list(str(bin(input2))[2:])
    binarysize=len(binaryl)
    binarylist=[int(binaryl[binarysize-1-i]) for i in range(binarysize)]
    modlist=[0 for i in range(binarysize)]
    modlist[0]=input1
    for i in range(1,binarysize):
        modlist[i]=times(modlist[i-1],modlist[i-1])
    answer=1
    for i in range(binarysize):
        if binarylist[i]==1:
            answer=times(answer,modlist[i])
    return answer
def divide(input1,input2):
    return times(input1,exponen(input2,characteristic-2))
N=int(input())
Fact=[1 for i in range(N+1)]
for i in range(1,N+1):
    Fact[i]=times(i,Fact[i-1])
ans=0
def C(A,B):
    return divide(Fact[A],times(Fact[B],Fact[A-B]))
SGL=[0 for i in range(N)]
for K in range(N):
    if 2*K-N<0:
        continue
    F=times(Fact[K],Fact[N-1-K])
    G=C(K-1,N-1-K)
    SGL[K]=(times(F,G))
for K in range(1,N):
    ans=plus(ans,times(SGL[K]-SGL[K-1],K))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
MOD = 10**9 + 7

def solve(f):
    n = f.read_int()

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

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

    ans = 0
    c0 = 0
    for i in xrange(n/2,n):
        c1 = (nCk(i-1, n-1-i)*fact[i]*fact[n-i-1] - c0 + MOD) % MOD
        ans = (ans + c1*i) % MOD
        c0 = (c0 + c1) % MOD

    return ans


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

MOD = 10**9 + 7

N = read()

NN = N + 1
fact = [1] * NN
for i in range(1, NN):
  fact[i] = (fact[i-1] * i) % MOD

def inv(n):
  return pow(n, MOD-2, MOD)

def comb(n, k):
  return fact[n] * inv(fact[n-k]) % MOD * inv(fact[k]) % MOD

S = [0] * N
for k in range((N+1) // 2, N):
  S[k] = comb(k-1, N-1-k) * fact[k] % MOD * fact[N-1-k] % MOD

ans = 0
for k in range((N+1) // 2, N):
  ans = (ans + k * (S[k] - S[k-1]) % MOD) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
import collections
n = int(input())
machine = []
[machine.append(i) for i in range(n-1)]
result = []
for i in permutations(machine):
    l = []
    [l.append(i) for i in range(n)]
    score = 0
    for j in i:
        l[j] = -1
        l[j + 1] = -1
        score += 1
        if(all(x == -1 for x in l)):
            result.append(score)
            break
temp = collections.Counter(result)
ans = 0
for k,v in temp.items():
    ans += k*v
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
nn = [i for i in range(0, n - 1)]
ans = 0
for element in itertools.permutations(nn, n - 1):
    field = 0
    tm = 0
    for elm in element:
        blk = (1 << elm) + (1 << (elm + 1))
        field = field | blk
        tm += 1
        if field == (1 << n) - 1:
            break
    ans += tm
    ans = ans % (10**9 + 7)
print(ans)