Problem:


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477



Sample input:
2 2


Sample output:
8

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
mod = 10**9+7

if abs(n -m) > 1:
    print(0)
    
if  abs(n -m) == 0:
    n_ans = 1
    m_ans = 1
    for i in range(1,n+1):
        n_ans *= i%mod
    for i in range(1,m+1):
        m_ans *= i%mod
    print(2*n_ans*m_ans%mod)
    
if abs(n -m) == 1:
    n_ans = 1
    m_ans = 1
    for i in range(1,n+1):
        n_ans *= i%mod
    for i in range(1,m+1):
        m_ans *= i%mod
    print(n_ans*m_ans%mod)

Submission status: Time Limit Exceeded

Solution code:
n,m = list(map(int, input().split()))
mod = 10**9+7

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

if abs(n-m)>1:
    print(0)
elif abs(n-m)==1:
    print(factorial(max(m,n))**2//max(m,n)%mod)
else:
    print(factorial(n)**2*2%mod)

Submission status: Time Limit Exceeded

Solution code:
n,m = list(map(int, input().split()))
mod = 10**9+7

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

if abs(n-m)>1:
    print(0)
elif abs(n-m)==1:
    print(factorial(n)*factorial(m)%mod)
else:
    print(factorial(n)*factorial(m)*2%mod)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ans = -1
mod = pow(10, 9) + 7

if abs(n-m) > 1:
    ans = 0
else:
    if abs(n-m) == 0:
        a = 1
        for i in range(1, n+1):
            a *= i
        ans = a*a*2
    if abs(n-m) == 1:
        a = 1
        for i in range(1, min(n, m)+1):
            a *= i
        b = a * max(n, m)
        ans = a * b

print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
S = input().split(" ")
N = int(S[0])
M = int(S[1])


def C(m, n):
    t = m
    s1 = 1
    while t > (m - n):
        s1 = s1 * t
        t = t - 1

    s2 = 1

    t = n
    while t >= 1:
        s2 = s2 * t
        t = t - 1

    return int(s1 / s2)


def A(m, n):
    s = 1
    t = m

    while t > m - n:
        s = s * t
        t = t - 1

    return s


def calculate(maxValue, minValue):
    if maxValue > minValue + 1:
        return 0

    if maxValue == minValue:
        S = A(maxValue,maxValue)
        return  2 * S * S

    return A(maxValue,maxValue) * A(minValue,minValue)

result = calculate(max(N,M),min(N,M))

print(result % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
hoge = 10 ** 9 + 7
def test():
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	while True:
		np = np * i_n
		mp = mp * i_m
		i_n -= 1
		i_m -= 1
		if i_n < 1:
			i_n = 1
		if i_m < 1:
			i_m = 1
		if i_n == 1 and i_m == 1:
			break
	s = 2 if n == m else 1
	r = (mp % hoge) * (np % hoge) * (s % hoge)
	print(r)

if __name__ == '__main__':
    test()

Submission status: Time Limit Exceeded

Solution code:
def test():
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	while True:
		np = np * i_n
		mp = mp * i_m
		i_n -= 1
		i_m -= 1
		if i_n < 1:
			i_n = 1
		if i_m < 1:
			i_m = 1
		if i_n == 1 and i_m == 1:
			break
	s = 2 if n == m else 1
	r = (mp*np*s) % (10**9 + 7)
	print(r)

if __name__ == '__main__':
    test()

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
if abs(n-m) > 1:
	print(0)
	exit()
i_n = n
i_m = m
np = 1
mp = 1
while True:
	np = np * i_n
	mp = mp * i_m
	i_n -= 1
	i_m -= 1
	if i_n < 1:
		i_n = 1
	if i_m < 1:
		i_m = 1
	if i_n == 1 and i_m == 1:
		break
s = 2 if n == m else 1
r = (mp*np*s) % (10**9 + 7)
print(r)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
if abs(n-m) > 1:
	print(0)
	exit()
i = n
np = 1
while True:
	np = np * i
	i -= 1
	if i == 1:
		break
i = m
mp = 1
while True:
	mp = mp * i
	i -= 1
	if i == 1:
		break
s = 2 if n == m else 1
r = (mp*np*s) % (10**9 + 7)
print(r)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = n-m
ans = 0
if abs(a) > 1:
    ans = 0
elif abs(a) == 1:
    x = 1
    y = 1
    for i in range(1,n+1):
        x *= i % (10**9+7)
    for i in range(1,m+1):
        y *= i % (10**9+7)
    ans = x * y %(10**9+7)
else:
    x = 1
    y = 1
    for i in range(1,n+1):
        x *= i % (10**9+7)
    for i in range(1,m+1):
        y *= i % (10**9+7)
    ans = 2*x * y %(10**9+7)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
mod=10**9+7
n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)
elif abs(n-m)==0:
    print(2*math.factorial(n)*math.factorial(m))
else:
    print(math.factorial(n)*math.factorial(m))

Submission status: Time Limit Exceeded

Solution code:
def frac(n):
  sum = 1
  for i in range(1,n+1):
    sum*=i
  return sum

import sys,fractions
N,M=map(int,input().split(' '))
# abs(N-M) > 1なら無理
if (abs(N-M) > 1):
  print(0)
  sys.exit()

# それぞれの階乗を求める
frac_n = frac(N)
frac_m = frac(M)

if N == M:
  ans = frac_m * frac_n * 2
else:
  ans = frac_n * frac_m

print(ans % (10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import math

n,m=map(int,input().split())
mod=10**9+7
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
elif abs(n-m)>1:
    a=0
print(a)

Submission status: Time Limit Exceeded

Solution code:
import math

n,m=map(int,input().split())
mod=10**9+7
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
elif abs(n-m)>1:
    print(0)
    exit()
print(a)

Submission status: Time Limit Exceeded

Solution code:
import math

n,m=map(int,input().split())
mod=10**9+7
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
if abs(n-m)>1:
    print(0)
    exit()
print(a)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
mod=10**9+7
import math
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
if abs(n-m)>1:
    print(0)
    exit()
print(a)

Submission status: Time Limit Exceeded

Solution code:
import math

a, b = map(int, input().split())
fa = math.factorial(a)
fb = math.factorial(b)

if abs(a-b) >= 2:
  print(0)
elif abs(a-b) == 1:
  print(fa*fb)
elif abs(a-b) == 0:
  print(2*(fa*fb))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>1:
    print(0)
    exit()
a,b=[1,1]
for i in range(1,min(n,m)+1):
    a*=i%mod
if n!=m:
    b*=max(n,m)*a
    print((a*b)%mod)
else:
    b=a
    print((2*a*b)%mod)

Submission status: Time Limit Exceeded

Solution code:
def kaijo(n):
    fact = 1
    for i in range(1, n+1):
        fact *=i
    return fact
    
n, m=map(int, input().split())
if n-m>=2 or m-n>=2:
    print(0)
elif n!=m:
    print(kaijo(m)*kaijo(n)%(10**9+7))
else:
    print(2*kaijo(m)*kaijo(n)%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
MOD = 10**9+7
a = abs(N-M)
if a>1:
    print(0)
    exit()
ans = 0
n=1
for i in range(1,N+1):
    n = n*i
m=1
for i in range(1,M+1):
    m = m*i
if N==M:
    ans = 2*(n*m)%MOD
else:
    ans = (n*m)%MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M = [int(i) for i in input().split()]
k = max(N,M)
l = min(N,M)
N = k
M = l
mod = 10**9+7
def frac(N):
    ans = 1
    for i in range(2,N+1):
        k = i % mod
        ans *= k
    return ans
if N - M > 1:
    print(0)
    exit()
if N == M:
    F = frac(N)
    f = F * F % mod
    print((2*f) % mod)
    exit()
if N == M+1:
    F1 = frac(M)
    F2 = frac(N)
    print((F1*F2) % mod)
    exit()

Submission status: Time Limit Exceeded

Solution code:
def kaijo(x):
    v=1
    for i in range(1,x+1):
        v*=i
    return v


n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>=2:
    ans=0
else:
    if n==m:
        s=kaijo(n)
        ans=2*s*s
    else:
        if n>m:
            s=kaijo(m)
            ans=s*s*n
        else:
            s=kaijo(n)
            ans=s*s*m

print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
def kaijo(x):
    v=1
    for i in range(1,x+1):
        v*=i
    return v


n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>=2:
    ans=0
else:
    if n==m:
        ans=2*kaijo(n)*kaijo(m)
    else:
        ans=kaijo(n)*kaijo(m)
print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
def factorial(n):
    p = 1
    for i in range(1, n+1):
        p *= i
    return p
n, m = map(int, input().split())
ans = 0
if abs(n-m) == 1:
    ans = factorial(n) * factorial(m)
elif n == m:
    ans = 2 * factorial(n) * factorial(m)
print(ans % (10 ** 9 + 7))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
a = 1

def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    for i in range(1,max(n,m)+1):
        count1 *= i
    '''
    for i in range(1,(min(n,m)+1)):
        count2 *= i
    '''

    print(count1*(count1//max(n,m))%(10**9+7))
else:
    for i in range(1,n+1):
        count1 *= i
    print(count1*count1*2%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    for i in range(1,max(n,m)+1):
        count1 *= i
    '''
    for i in range(1,(min(n,m)+1)):
        count2 *= i
    '''
    print(count1*(count1//max(n,m))%(10**9+7))
else:
    for i in range(1,n+1):
        count1 *= i
    print(count1*count1*2%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    for i in range(1,max(n,m)+1):
        count1 *= i
    for i in range(1,(min(n,m)+1)):
        count2 *= i
    print(count1*count2%(10**9+7))
else:
    for i in range(1,n+1):
        count1 *= i
    print(count1*count1*2%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    #print((saiki(max(n,m))*saiki(min(n,m)))%(10**9+7))
    for i in range(1,max(n,m)+1):
        count1 *= i%(10**9+7)
    for i in range(1,(min(n,m)+1)):
        count2 *= i%(10**9+7)
    print(count1*count2)
else:
    #print((saiki(n)*saiki(m)*2)%(10**9+7))
    for i in range(1,n+1):
        count1 *= i%(10**9+7)
    print(count1*count1*2)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
ans=int(1)
for i in range(1,n+1):
    ans*=i
    
for i in range(1,m+1):
    ans*=i

if n==m:
    ans*=2

if abs(n-m)>1:
    ans=0

print(ans%1000000007)

Submission status: Time Limit Exceeded

Solution code:
import math
m,n=map(int,input().split())
f=abs(m-n)
if f == 0:
  ans=2*math.factorial(m)*math.factorial(n)
  print(ans)
  exit()
elif f == 1:
  ans=math.factorial(m)*math.factorial(n)
  print(ans)
  exit()
else:
  print(0)
  exit()

Submission status: Time Limit Exceeded

Solution code:
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
n,m = map(int,input().split())
if n == m:
    ans = P(n,n) * P(m,m) * 2
    print(ans)
elif abs(n-m) > 1:
    print(0)
else:
    ans = P(n,n)*P(m,m)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(100000)


def k(x):
    if x < 2:
        return 1
    return x * k(x - 1)


n, m = map(int, input().split())
w = 10 ** 9 + 7
if n + 1 == m or m + 1 == n:
    print(k(n) * k(m) % w)
elif n == m:
    print(2 * k(n) ** 2 % w)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
def factorial(n):
  fact = 1
  for i in range(2, n+1):
    fact *= i%(10**9+7)
  return fact

N, M = map(int, input().split())
MOD = 10**9+7
if N == M:
  print(2*factorial(N)**2 % MOD)
elif abs(N-M) == 1:
  print(factorial(min(N, M))**2 * max(N, M) % MOD)
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
def lam(x):
	ans=1
	for i in range(1,x+1):
		ans*=i
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n)*lam(m)%mod)
else:
	print(2*lam(n)**2%mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

N,M = map(int, input().split())

if abs(N-M) > 1:
    print(0)
    exit()

def frac(n):
    if n == 1: return 1
    return n * frac(n-1)

n_frac = frac(N)
m_frac = frac(M)
if N-M == 0:
    print((n_frac*m_frac)*2)
else:
    print(n_frac*m_frac)

Submission status: Time Limit Exceeded

Solution code:
import math
n,m=[int(i) for i in input().split()]
if(abs(n-m)>1):
    print(0)
    exit()

def is_even(n):
    if(n%2==0):
        return True
    else:
        return False
if(is_even(n) and is_even(m) or not is_even(n) and not is_even(m)):
    print(math.factorial(n)*math.factorial(m)*2)
    exit()
else:
    print(math.factorial(n)*math.factorial(m))
    exit()
# if(is_even(n) and is_even(m)):
#     print(math.factorial(n)*math.factorial(m)*2)
#     exit()
# elif(is_even(n) and not is_even(m)):
#     print(math.factorial(n)*math.factorial(m))
#     exit()
# elif(not is_even(n) and is_even(m)):
#     print(math.factorial(m)*math.factorial(n))
#     exit()
# elif(not is_even(n) and not is_even(m)):
#     print(math.factorial(m)*math.factorial(n)*2)
#     exit()

Submission status: Time Limit Exceeded

Solution code:
import math
n,m=[int(i) for i in input().split()]
if(abs(n-m)>1):
    print(0)
    exit()
    
def is_even(n):
    if(n%2==0):
        return True
    else:
        return False

if(is_even(n) and is_even(m)):
    print(math.factorial(n)*math.factorial(m)*2)
    exit()
elif(is_even(n) and not is_even(m)):
    print(math.factorial(n)*math.factorial(m))
    exit()
elif(not is_even(n) and is_even(m)):
    print(math.factorial(m)*math.factorial(n))
    exit()
elif(not is_even(n) and not is_even(m)):
    print(math.factorial(m)*math.factorial(n)*2)
    exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt=1
for i in range(1,n+1):
  cnt*=i
if n==m:
  print(2*((cnt**2)%((10**9)+7)))
if n-m==1:
  print((cnt*(cnt//n))%((10**9)+7))
if m-n==1:
  print((cnt*cnt*m)%((10**9)+7))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin,setrecursionlimit
import sys
from math import factorial
setrecursionlimit(10**6)
input=stdin.readline

a,b=map(int,input().split())
if abs(a-b)>1:
    print(0)
else:
    ans=factorial(a)*factorial(b)
    print(ans if abs(a-b)==1 else ans*2)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    SSSS = 10 ** 9 + 7
    [A, B] = string_to_int(inputs[0])
    max_len = max([A, B])
    min_len = min([A, B])
    diff_len = abs(A-B)
    if diff_len >= 2:
        return 0

    pattern = 0
    for i in reversed(range(1, min_len + 1)):
        if i == min_len:
            if diff_len == 1:
                pattern = max_len * min_len
                pattern *= min_len
            else:
                pattern = min_len + min_len
                pattern *= min_len
        else:
            pattern *= i * i
    #     if pattern >= SSSS:
    #         patterns.append(pattern)
    #         pattern = 1

    # if len(patterns) >= 1:
    #     pattern = 1
    #     for p in patterns:
    #         pattern *= p

    return pattern % SSSS


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys


N, M = map(int, input().split())

nn = max(N, M)
mm = min(N, M)

if mm != nn and mm + 1 < nn:
    print(0)
    sys.exit()

if N == M:
    print(math.factorial(N)*math.factorial(M) * 2)
else:
    print(math.factorial(N)*math.factorial(M))

Submission status: Time Limit Exceeded

Solution code:
import math 

INF = 1e9+7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)
if n==m:
    ans = 2*(a**2)
elif n==m-1 or n-1 ==m:
    ans = a*a//n
else:
    ans = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n,m = map(int,input().split())
a = abs(n - m)
ans = 0
if a == 0:
	ans = math.factorial(n) * math.factorial(m) * 2
elif a == 1:
	ans = math.factorial(n) * math.factorial(m) 
else:
	ans = 0
print(ans)

Submission status: Time Limit Exceeded

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

def cnt(a): #階乗の計算
    res = 1
    for i in range(1,a+1):
        res *= i

    return res


if abs(n-m) >= 2:
    print(0)

else:
    if n==m:
        ans1 = cnt(n)
        ans2 = cnt(m)
        print(2*ans1*ans2)
    

    else:
        ans1 = cnt(n)
        ans2 = cnt(m)
        print(ans1*ans2)

Submission status: Time Limit Exceeded

Solution code:
def fact(X):
    res = 1
    for i in range(1,X+1):
        res *= i
    return res

N, M = map(int,input().split())
N, M = max(N,M), min(N,M)
mod = 10**9 + 7
if N-M != 1 and N != M:
    ans = 0
elif N != M:
    factN = fact(N)
    ans = (factN*factN//N)%mod
else:
    factN = fact(N)
    ans = (factN*factN*2)%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def fact(X):
    res = 1
    for i in range(1,X+1):
        res *= i
    return res

N, M = map(int,input().split())
N, M = max(N,M), min(N,M)
mod = 10**9 + 7
if N-M != 1 and N != M:
    ans = 0
elif N != M:
    ans = (fact(N)*fact(M))%mod
else:
    ans = (fact(N)*fact(M)*2)%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N, M = map(int, input().split())
ans = 1
if N == M:
    ans *= 2
if not (N+1==M or N == M+1 or N==M):
    print(0)
else:
    for i in range(N):
        ans *= i+1
    for i in range(M):
        ans *= i+1
    print(ans%1000000007)

Submission status: Time Limit Exceeded

Solution code:
# C - Reconciled?
N, M = map(int, input().split())
ans_N = 1
ans_M = 1

if abs(N - M) >= 2:
    print(0)
else:
    for i in range(N):
        ans_N_1 = (M-i)*(N-i) % (10**9+7)
        ans_N = ans_N * ans_N_1

    for j in range(M):
        ans_M_1 = (M-j)*(N-j) % (10**9+7)
        ans_M = ans_M * ans_M_1
    print(ans_N+ans_M)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N, M = map(int, input().split())
np = len(list(itertools.permutations(list(range(N))))) % (10**9+7)
mp = len(list(itertools.permutations(list(range(M))))) % (10**9+7)
if N == M:
    ans = np*mp % (10**9+7)*2
elif abs(N - M) == 1:
    ans = np * mp % (10 ** 9 + 7)
else:
    ans = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
mod = 10**9 + 7
num = abs(N - M)

if num > 1:
    print(0)
    exit()

n = 1
for i in range(1, N+1):
    n *= i

if N == M:
    m = n
    ans = (2 * n * m) % mod
else:
    if N < M:
        m = M * n
    else:
        m = n // N
    ans = (n * m) % mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
mod = 10**9+7

def kaizyou(x):
    y = 1
    while x != 0:
        y *= x
        x -= 1
    return y%mod


N , M = map(int, input().split())
if abs(N-M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * kaizyou(N) ** 2
elif N > M:
    ans = N * kaizyou(M) ** 2
else:
    ans = M * kaizyou(N) ** 2
print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
total=1
mod=1000000007
if N==M:
    for i in range(1,N+1):
        total=total*i*i
    print((total*2)%mod)

elif N+1==M or M+1==N:
    total*=max(M,N)
    for i in range(1,min(M,N)+1):
        total=total*i*i
    print(total%mod)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())

a = 1
b = 1
if N == M:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a

elif N - M == 1:
    for i in range(1,M+1):
        b *= i % 1000000007
    a = b * N 
elif M - N == 1:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a * M 
    

if abs(N-M) == 1:
    ans = a * b %1000000007
elif N == M:
    ans = 2*a*b % 1000000007
else:
    ans = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())

a = 1
b = 1
for i in range(1,N+1):
    a *= i % 1000000007
for i in range(1, M+1):
    b *= i % 1000000007

if abs(N-M) == 1:
    ans = a * b %1000000007
elif N == M:
    ans = 2*a*b % 1000000007
else:
    ans = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())

div=10**9+7
n=1
m=1
diff=abs(N-M)
for i in range(1,N+1):
    n*=i%div
for i in range(1,M+1):
    m*=i%div

if diff>1:
    ans=0
elif diff==0:
    ans=(2*n*m)%div
elif diff==1:
    ans=(n*m)%div
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())

div=10**9+7
n=1
m=1
diff=abs(N-M)
for i in range(N):
    n*=(i+1)%div
for i in range(M):
    m*=(i+1)%div

if diff>1:
    ans=0
elif diff==0:
    ans=(2*n*m)%div
elif diff==1:
    ans=(n*m)%div
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())

div=10**9+7
tmp=1
K=min(N,M)
diff=abs(N-M)
for i in range(K):
    tmp*=(i+1)%div
ans=max(0,(2-diff)*tmp*tmp*(1+K*diff))%div
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=(int(i) for i in input().split())
#print(n,m)

mod=10**9+7
ans=1

if abs(n-m) > 1:
  print(0)
elif n==m:
  while n > 0:
    ans*=n%mod
    n-=1
  ans=ans*ans*2%mod
  print(ans)
else:
  if n < m:
    n,m=m,n
  while n > 0:
    ans*=n%mod
    n-=1
  ans=ans*ans//(m+1)%mod
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N, M = list(map(int, input().split()))

l = abs(N - M)

# 列を作れないとき
if l >= 2:
    print(0)
    exit()

if l == 1:

    if N < M:
        _n = math.factorial(N)
        _m = _n * (M)
        ans = _n * _m
        print(ans)
        exit()

    else:
        _m = math.factorial(M)
        _n = _m * (N)
        ans = _n * _m
        print(ans)
        exit()



_n = math.factorial(N)
# _m = factorial(M)

ans = (_n * _n * 2) % (10**9+7)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))

l = abs(N - M)

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


# 列を作れないとき
if l >= 2:
    print(0)
    exit()

if l == 1:

    if N < M:
        _n = factorial(N)
        _m = _n * (M)
        ans = _n * _m
        print(ans)
        exit()

    else:
        _m = factorial(M)
        _n = _m * (N)
        ans = _n * _m
        print(ans)
        exit()



_n = factorial(N)
# _m = factorial(M)

ans = (_n * _n * 2) % (10**9+7)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))

l = abs(N - M)

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


# 列を作れないとき
if l >= 2:
    print(0)
    exit()

if l == 1:
    _n = factorial(N)
    _m = factorial(M)
    ans = _n * _m
    print(ans)
    exit()


_n = factorial(N)
_m = factorial(M)

ans = (_n * _m * 2) % (10**9+7)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
if abs(n-m) != 0 and abs(n-m) != 1 :
    print(0)
else:
    x = 1
    for i in range(1,min(n,m)+1):
        x = x * i

    if abs(n-m) == 0:
        print((2 * x * x)%(10**9+7))
    else:
        print((x * x * max(n,m))%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import math

nm = input().split(" ");
n = int(nm[0]);
m = int(nm[1]);
if n - m == 1 or m - n == 1:
  ansn = math.factorial(n);
  ansm = math.factorial(m);
  print(ansn*ansm);
elif n == m :
  ansn = math.factorial(n);
  ansm = math.factorial(m);
  print(ansn*ansm*2);
else :
  print(0);

Submission status: Time Limit Exceeded

Solution code:
N, M = sorted(list(map(int, input().split())))

gap = M - N
if gap >= 2:
	ans = 0
else:
	ans = 1
	for i in range(1, M+1):
		ans *= i
	for i in range(1, N+1): 
		ans *= i
	if gap == 0:
		ans *= 2
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
a=list(map(int,input().split()))
ans=0
i=0
if max(a)-min(a)>=2:
    pass
else:
    ans=1
    i=1
    if a[0]==a[1]:
        while i!=max(a)+1:
            ans=ans*i
            i=i+1
        ans=ans*ans*2
    else:
        while i!=max(a)+1:
            ans=ans*i
            i=i+1
        ans=ans*ans/max(a)
        #print(ans)

print(int(ans%1000000007 ))

Submission status: Time Limit Exceeded

Solution code:
def fct(n):
	v = 1
	for i in range(1, n+1):
		v = v*i
	return v
N, M = map(int, input().split())
ans = 0
if N == M:
	v = fct(N)
	ans = (v*v*2)%1000000007
elif abs(N-M) == 1:
	ans = (fct(N)*fct(M))%1000000007
print(ans)

Submission status: Time Limit Exceeded

Solution code:
M, N = map(int, input().split())
def fraction(N):
  a = N
  for x in range(N-1,1,-1):
    a *= x
  return a

p = abs(M - N)
if p < 2:
  print(fraction(M) * fraction(N) * (2 - p) % (1000000007))
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc065/tasks/arc076_a

import itertools
from collections import Counter
from collections import defaultdict
import bisect
import math
import heapq

def main():
    N, M = map(int, input().split())

    mod = 10**9 + 7

    if abs(N - M) > 1:
        print(0)
        exit()

    n = 1
    for i in range(1, N + 1):
        n *= i
    m = 1
    for i in range(1, M + 1):
        m *= i

    if N == M:
        print((n * m * 2) % mod)
    else:
        print((n * m) % mod)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



D = 10**9+7

def main():
  NM = sorted(list(map(int, input().split())))
  if NM[1]-NM[0] > 1:
    return 0
  elif NM[0]==NM[1]:
    ret = 2*(math.factorial(NM[0])**2) % D
    return ret
  else:
    ret = math.factorial(NM[0])*math.factorial(NM[1])
    return ret

if __name__ == "__main__":
  print(main())

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
from sys import exit, stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
p_n = len(list(permutations(i for i in range(N))))
p_m = len(list(permutations(i for i in range(M))))
if abs(N-M)==0:
    print(2*p_n*p_m)
elif abs(N-M)==1:
    print(p_n*p_m)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import math
n,m=map(int,input().split())
ans=math.factorial(m)*math.factorial(n)
if n==m:
  ans*=2
elif abs(n-m)>1:
  ans=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n,m=map(int,input().split())
ans=math.factorial(m)*math.factorial(n)
if n==m:
  ans*=2
elif abs(n-m)>1:
  ans=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N, M = map(int, input().split())
N, M = max(N,M), min(N,M)

if N == M:
    ans = math.factorial(N) ** 2 * 2
elif N - M == 1:
    ans = math.factorial(N) * math.factorial(M)
else:
    ans = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [-1 for i in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

for i in range(1, max(n, m) + 1):
    if memo[i] != -1:
        continue
    elif i == 1:
        memo[i] = 1
    else:
        memo[i] = i * memo[i - 1]

if abs(n - m) >= 2:
    print(0)
else:
    nn = memo[n] % mod
    mm = memo[m] % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split())
import math
if a==b:
  print(math.factorial(a)**2*2%(10**9+7))
elif abs(a-b)==1:
  print(math.factorial(max(a,b))**2//max(a,b))
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:

a,b = map(int,input().split())
import math
if a==b:
  print(math.factorial(a)**2*2%(10**9+7))
elif abs(a-b)==1:
  print(math.factorial(a)*math.factorial(b))
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
MOD = 1000000007
a, b = n, m
count_n = n
count_m =m
count = 0
if abs(n-m) == 1:
  for _ in range(n-1):
    n -= 1
    count_n *= (n % MOD)
  for _ in range(m-1):
    m -= 1
    count_m *= (m % MOD)
  count = (count_m % MOD) * (count_n % MOD)
elif abs(n-m) == 0:
  for _ in range(n-1):
    n -= 1
    count_n *= (n % MOD)
  for _ in range(m-1):
    m -= 1
    count_m *= (m % MOD)
  count = 2 * (count_m % MOD) * (count_n % MOD)
  count %= MOD
else:
  count = 0

print(count)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
n,m=map(int,input().split())
if n==m:
    print(2*factorial(n)*factorial(m))
elif n==m+1 or m==n+1:
        print(factorial(n)*factorial(m))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
def Factorial(A):
    X = 1
    for num in range(1, A+1):
        X *= num
    return X

n, m = map(int, input().split())

if n == m+1:
    ans = Factorial(n)*Factorial(m)
elif n == m:
    ans = Factorial(n)*Factorial(m)*2
elif n+1 == m:
    ans = Factorial(n)*Factorial(m)
else:
    ans = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
    ans%=1000000007
    return ans

if N==M:
    p=2*(bikkuri(N)*bikkuri(M)%1000000007)
    p%=1000000007
elif  abs(N-M)==1:
    p=(bikkuri(M)*bikkuri(N)%1000000007)
    p%=1000000007
else:
    p=0
print(p)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
    ans%=1000000007
    return ans

if N==M:
    p=2*bikkuri(N)*bikkuri(M)
    p%=1000000007
elif  abs(N-M)==1:
    p=bikkuri(M)*bikkuri(N)
    p%=1000000007
else:
    p=0
print(p)