Problem: 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.

Submission attempts (in order):
n = int(input())
p = 10**9 + 7

def fact(n, p):
    n_ = [1]
    for i in range(1, n+1):
         n_.append((n_[-1]*i) % p)
    return n

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

ans = 0
m = n - 1
f = list(fact(m, p))
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] %p * f[k] % p
    ans += (perm_ - perm) %p * k % p
    ans %= p
    perm = perm_

print(ans)

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


#     n_ = 1
#     yield n_

#          n_ = (n_*i) % p
#          yield n_



#     yield m


#         yield m

def fact(n, p):
    n_ = [1]
    for i in range(1, n+1):
         n_.append((n_[-1]*i) % p)
    return n

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

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

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

print(ans)

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

def fact(n, p):
    n_ = [1]
    for i in range(1, n+1):
         n_.append((n_[-1]*i) % p)
    return n

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

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

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

print(ans)

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)

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

def fact(n, p):
    n_ = [1]
    for i in range(1, n+1):
         n_.append((n_[-1]*i) % p)
    return n_

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

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

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

print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted