Problem:
 Score : 600 points    Problem Statement Let us define the oddness of a permutation p = { p_1,\ p_2,\ ...,\ p_n } of { 1,\ 2,\ ...,\ n } as \sum_{i = 1}^n |i - p_i| .  Find the number of permutations of { 1,\ 2,\ ...,\ n } of oddness k , modulo 10^9+7 .      Constraints  All values in input are integers.  1 \leq n \leq 50  0 \leq k \leq n^2         Input Input is given from Standard Input in the following format:  n  k       Output Print the number of permutations of { 1,\ 2,\ ...,\ n } of oddness k , modulo 10^9+7 .        Sample Input 1 3 2      Sample Output 1 2  There are six permutations of { 1,\ 2,\ 3 }. Among them, two have oddness of 2 :  { 2,\ 1,\ 3 } and { 1,\ 3,\ 2 }.       Sample Input 2 39 14      Sample Output 2 74764168

Solution (Python):
MOD = 10**9 + 7
n, oddness = [int(item) for item in input().split()]

if oddness % 2 ==  1:
    print(0)
    exit()
oddness //= 2
max_oddness = 1250 + n
max_pending = n//2 + 2

dp = [[[0] * (max_oddness) for _ in range(max_pending)] for _ in range(n)]
dp[0][0][0] = 1
dp[0][1][1] = 1
for i in range(1, n):
    for j in range(min(i+1, n//2+1)):
        for k in range(oddness+1):
            dp_ijk_prev = dp[i-1][j][k]
            
            
            dp[i][j+1][k+j+1] += dp_ijk_prev 
            
            if j > 0:
                dp[i][j-1][k+j-1] += dp_ijk_prev * j*j
            
            dp[i][j][k+j] += dp_ijk_prev
            
            dp[i][j][k+j] += dp_ijk_prev * j * 2
            dp[i][j][k+j] %= MOD

print(dp[n-1][0][oddness] % MOD)

Solution (Python):
MOD = 10**9 + 7
n, oddness = [int(item) for item in input().split()]

if oddness % 2 ==  1:
    print(0)
    exit()
oddness //= 2
max_oddness = 1300 + n
max_pending = n//2 + 2

dp = [[[0] * (max_oddness) for _ in range(max_pending)] for _ in range(n)]
dp[0][0][0] = 1
dp[0][1][1] = 1
for i in range(1, n):
    for j in range(min(i+1, max_pending-1)):
        for k in range(oddness+1):
            dp_ijk = dp[i-1][j][k]
            
            
            dp[i][j][k+j] += dp_ijk * (1 + j*2)
            dp[i][j][k+j] %= MOD
            
            if j > 0:
                dp[i][j-1][k+j-1] += dp_ijk * j*j
            
            dp[i][j+1][k+j+1] += dp_ijk 

print(dp[n-1][0][oddness] % MOD)

Solution (Python):
MOD = 10**9 + 7
n, oddness = [int(item) for item in input().split()]

if oddness % 2 ==  1:
    print(0)
    exit()
oddness //= 2
max_oddness = n*n + n
max_pending = n + 1

dp = [[[0] * (max_oddness) for _ in range(max_pending)] for _ in range(n)]
dp[0][0][0] = 1
dp[0][1][1] = 1
oddness_itr = [item for item in range(n*n)]
for i in range(1, n):
    for j in range(i+1):
        
        if j > n // 2:
            break
        for k in oddness_itr:
            
            
            dp[i][j][k+j] += dp[i-1][j][k]
            
            dp[i][j][k+j] += dp[i-1][j][k] * j*2 
            dp[i][j][k+j] %= MOD
            
            if j > 0:
                dp[i][j-1][k+j-1] += dp[i-1][j][k] * j*j
                dp[i][j-1][k+j-1] %= MOD 
            
            dp[i][j+1][k+j+1] += dp[i-1][j][k] 
            dp[i][j+1][k+j+1] %= MOD 

print(dp[n-1][0][oddness])

Solution (Python):
MOD = 10**9 + 7
n, oddness = [int(item) for item in input().split()]

if oddness % 2 ==  1:
    print(0)
    exit()
oddness //= 2
max_oddness = n*n + n
max_pending = n + 1

dp = [[[0] * (max_oddness) for _ in range(max_pending)] for _ in range(n)]
oddness_itr = [item for item in range(n*n)]
for i in range(n):
    if i == 0:
        dp[0][0][0] = 1
        dp[0][1][1] = 1
        continue
    for j in range(i+1):
        for k in oddness_itr:
            
            
            dp[i][j][k+j] += dp[i-1][j][k]
            
            dp[i][j][k+j] += dp[i-1][j][k] * j*2 
            dp[i][j][k+j] %= MOD
            
            if j > 0:
                dp[i][j-1][k+j-1] += dp[i-1][j][k] * j*j
                dp[i][j-1][k+j-1] %= MOD 
            
            dp[i][j+1][k+j+1] += dp[i-1][j][k] 
            dp[i][j+1][k+j+1] %= MOD 

print(dp[n-1][0][oddness])

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n,m = [int(i) for i in readline().split()]
MOD = 10**9+7



dp = [[[0 for k in range(n*n+1)] for j in range(n+1)] for i in range(n+1)]

dp[0][0][0] = 1

for i in range(n):
    for j in range(i+1):
        for k in range(n*n):
            if k + 2*j >= n*n: continue
            dp[i+1][j+1][k+2*j] += dp[i][j][k]
            dp[i+1][j+1][k+2*j] %= MOD
            dp[i+1][j  ][k+2*j] += (2*j+1)*dp[i][j][k]
            dp[i+1][j  ][k+2*j] %= MOD
            dp[i+1][j-1][k+2*j] += j*j*dp[i][j][k]
            dp[i+1][j-1][k+2*j] %= MOD
    

ans = dp[-1][0][m]
print(ans)