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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
mod = 1000000000 + 7
def func(n):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= mod

    return r
N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    res = func(A)
    print(int(res**2/A)%mod)
else:
    print(int(2*func(N)**2)%mod)

Submission status: Wrong Answer

Solution code:
nums = [1, 1]
def step_div(n):
	i = len(nums)
	while i <= n:
		nums.append(nums[i-1] * i)
		i += 1
	return nums[n]

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

remain = abs(n-m)
if remain <= 1:
	print((step_div(n) * step_div(m) * 2) % (10**9+7))
else:
	print(0)

Submission status: Wrong Answer

Solution code:
def fact(n: int, mod: int) -> int:
    res = 1
    for i in range(n,1,-1):
        res = (res * i) % mod
        
    return res % mod

def judge(n:int, m:int, mod:int) -> int:
    if abs(n-m) > 2:
        return 0
    elif abs(n-m) == 1:
        return (fact(n,mod) * fact(m,mod)) % mod
    else:
        return (2 * fact(n,mod) * fact(m,mod)) % mod
    
N, M = map(int, input().split())
MOD = 10**9+7

ans = judge(N,M,MOD)

print(ans)

Submission status: Wrong Answer

Solution code:
def fact(n: int, mod: int) -> int:
    res = 1
    for i in range(n,1,-1):
        res = (res * i) % mod
        
    return res

def judge(n:int, m:int, mod:int) -> int:
    if abs(n-m) > 2:
        return 0
    elif abs(n-m) == 1:
        return fact(n,mod) * fact(m,mod) % mod
    else:
        return (2 * fact(n,mod) * fact(m,mod)) % mod
    
N, M = map(int, input().split())
MOD = 10**9+7

ans = judge(N,M,MOD)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

n,m=map(int,input().split())
a=math.factorial(n) * math.factorial(m) 
if abs(n-m) == 0:
    a *= 2
print(a%1000000007)

Submission status: Wrong Answer

Solution code:
import math

n,m=map(int,input().split())
a=math.factorial(n) * math.factorial(m) % 1000000007
if abs(n-m) == 0:
    a *= 2
print(a)

Submission status: Wrong Answer

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

def fa(N):
    p = 1
    for i in range(1,N+1):
        p = (p * i)%(10**9+7)
    return p
if abs(N-M) > 2:
    print(0)
elif abs(N-M) == 1:
    print((fa(N)*fa(M))%(10**9+7))
else:
    print((2*fa(N)*fa(M))%(10**9+7))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

def f():
  if abs(n - m) > 1:
    return -1
  return math.factorial(n) * math.factorial(m) * (2 if n == m else 1)
print(f() % (10**9 + 7))

Submission status: Wrong Answer

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

mod = 10**9+7
Ans = -1

if abs(N-M) == 0:
    t = 1
    for i in range(1,N+1):
        t = (t*i)%mod
    Ans = (2*t*t)%mod
elif abs(N-M) == 1:
    t1,t2 = 1,1
    for i in range(1,N+1):
        t1 = (t1*i)%mod
    for i in range(1,M+1):
        t2 = (t2*i)%mod
    Ans = (t1*t2)%mod

print(Ans)

Submission status: Wrong Answer

Solution code:
import math

n,m = map(int,raw_input().split())
mod = 10**9+7
if abs(n-m) > 1:
	print 0

elif n-m ==1:
	print (math.factorial(n)*math.factorial(m))%mod

else :
	print (math.factorial(n)*math.factorial(m)*2)%mod

Submission status: Wrong Answer

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

mod = (10**9 + 7)

def factorial(n):
    for i in range(1, n):
        n *= i
        if n > mod:
            n %= mod
    return n

if N == M:
    print(factorial(N) * factorial(M) * 2 % mod)
elif abs(N-M) == 1:
    a = factorial(M)
    print(a**2%mod)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math
N, M = map(int, input().split())
if abs(N-M)>1:
    print(0)
elif N!=M:
    ans = (math.factorial(N)%1000000007)*(math.factorial(M)%1000000007)
    print(ans)
else:
    ans = (math.factorial(N)%1000000007)*(math.factorial(M)%1000000007)*2%1000000007
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
N,M=map(int,input().split())
def f(n):
    n=math.factorial(n)
    return n
if abs(N-M)==1:
    s=f(N)*f(M)**2
elif N==M:
    s=2*f(N)**2
else:
    s=0
print(s%(10**9+7))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
import math
N,M=map(int,input().split())
def f(n):
    n=math.factorial(n)
    return n
if N==M+1:
    s=N*f(M)**2
elif M==N+1:
    s=M*f(N)**2
elif N==M:
    s=f(N)**2
else:
    s=0
print(s%(10**9+7))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
def f(n):
    for i in range(n-1):
        n=n*(i+1)
    return n
if N==M+1:
    s=N*2*f(M)
elif M==N+1:
    s=M*2*f(N)
elif N==M:
    s=2*f(N)
else:
    s=0
print(s%(10**9+7))

Submission status: Wrong Answer

Solution code:
import math
n,m = list(map(int,input().split()))
a = 0
b = 0

if n > m:
    a = n
    b = m
elif n < m :
    a = m 
    b = n
else:
    a = n
    b = n
    
if abs(n-m) ==1:
    print(((math.factorial(a) % 1000000007) * (math.factorial(b) % 1000000007)))
elif n ==m:
    print(((math.factorial(a) % 1000000007) * (math.factorial(b) % 1000000007)) *2 % 1000000007)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys

res=10**9+7
def permutation(n):
  x=1
  for i in range(n):x=(x*(i+1))%res
  return(x%res)
n,m=map(int,input().split())
n%=res
m%=res
if(n>m+1 or m>n+1):
  print(0)
elif(n==m):
  p=permutation(n)
  print((2*p*p)%res)
else:
  ma=max(n,m)
  p=permutation(ma)
  p2=p//ma
  print((p*p2)%res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

n, m = map(int, input().split())
fac_m = 1
fac_n = 1

for i in range(1, m + 1):
    fac_m *= i
    fac_m = fac_m % 1000000007

for j in range(1, n + 1):
    fac_n *= j
    fac_n = fac_n % 1000000007

if (n + m) % 2 == 0:
    print((fac_n * fac_m * 2) % 1000000007)
elif abs(n - m) >= 2:
    print(0)
else:
    print((fac_m * fac_n) % 1000000007)

Submission status: Wrong Answer

Solution code:
import math
N, M =map(int,input().split())
ans=0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
(n, m), mod = map(int, raw_input().split()), 10 ** 9 + 7
def p(n, m):
    ans = 1
    for i in xrange(1, n + 1):
        ans = ((ans % m) * (i % m) % m)
    return ans
print (p(n, mod) * p(m, mod) % mod) if (n + m) % 2 else (p(n, mod) * p(m, mod) * 2) % mod

Submission status: Wrong Answer

Solution code:
MOD = int(1e9)+7

def fact(n):
    res = 1
    for x in range(1, n+1):
        res *= x
        res %= MOD
    return res
        

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

if abs(N - M) >= 2:
    print(0)
elif N == M:
    print(2 * fact(N) * fact(N))
else:
    print(fact(N)*fact(M))

Submission status: Wrong Answer

Solution code:
import math

n,m= (int(i) for i in input().split())

if n==m:
    a = math.factorial(n)
    print(2 * a * a % (100000007))
elif abs(n-m) == 1:
    print( (math.factorial(n) * math.factorial(m)) % (100000007))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math

a, b = (int(i) for i in input().split())
_n = 10**9 + 7
if abs(a-b) == 1:
    ab = math.factorial(a) * math.factorial(b)
elif a == b:
    ab = 2*(math.factorial(a) + math.factorial(b))
else:
    ab = 0

print(ab%_n)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
if -2 < (n - m) < 2:
  ans = 1
  for i in range(n):
    ans *= i
    ans %= 1000000007
  for i in range(m):
    ans *= i
    ans %= 1000000007
  if n != m:
    print(ans)
  else:
    print(((ans) * 2) % 1000000007)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
if -2 < (n - m) < 2:
  ans = 1
  for i in range(n):
    ans *= i
    ans %= 1000000007
  for i in range(m):
    ans *= i
    ans %= 1000000007
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
if -2 < (n - m) < 2:
  ans = 1
  for i in range(n):
    ans *= n
    ans %= 1000000007
  for i in range(m):
    ans *= n
    ans %= 1000000007
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
from math import factorial

a, b = [int(i) for i in input().split()]
num = 1
numb = 1
if abs(a-b) == 1:
    num = factorial(a)*factorial(b)%(10**9+7)
elif abs(a-b) == 0:
    num = factorial(a) * factorial(b) % (10 ** 9 + 7)*2
else:
    num = 0

print(num)

Submission status: Wrong Answer

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

import sys
a,b=map(int,input().split())
ans=0
if a==b+1 or b:
    ans=((a-b+2)*kai(a)*kai(b))%(10**9+7)
elif b==a+1:
    ans=((b-a+2)*kai(a)*kai(b))%(10**9+7)
else:
    print(0)
    sys.exit()
print(ans)

Submission status: Wrong Answer

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

a,b=map(int,input().split())
ans=0
if a>=b:
    ans=((a-b+2)*kai(a)*kai(b))%(10**9+7)
else:
    ans=((b-a+2)*kai(a)*kai(b))%(10**9+7)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
l = list(map(int, input().split()))
n = l[0] - l[1]
if n == 0:
    print(((math.factorial(l[0])**2) * 2) % 1000000007)
elif n == 1:
    print((math.factorial(l[0]) * math.factorial(l[1]))% 1000000007)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

MOD = int(1000000000+7)

table = list(-1 for i in range(100000+1))

def factorial(n):
    res = n
    for i in reversed(range(1, n)):
        if table[i] == -1:
            res = (res % MOD) * (i % MOD)
        else:
            res = (res % MOD) * (table[i] * MOD)
            break
    table[n] = res
    return res

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

diff = abs(N-M)
if diff == 0:
    print((2 * factorial(N) * factorial(M)) % MOD)
elif diff == 1:
    print((factorial(N) * factorial(M)) % MOD)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

MOD = int(1000000000+7)

table = list(-1 for i in range(100000+1))

def factorial(n):
    res = n
    for i in reversed(range(1, n)):
        if table[i] == -1:
            res = (res % MOD) * (i % MOD)
        else:
            res = (res % MOD) * (table[i] * MOD)
            break
    table[n] = res
    return res

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

diff = abs(N-M)
if diff == 0:
    print(2 * (factorial(N) * factorial(M)) % MOD)
elif diff == 1:
    print((factorial(N) * factorial(M)) % MOD)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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


if abs(n - m) >=2:
    print(0)
else:
    print(ans%(1000000007))

Submission status: Wrong Answer

Solution code:
def factorial(n) : 
    a = 1
    for i in range(1 , n+1):
        if(i == 50000):
            return a * 737935835
        a *= i
    return a

def solve():
    (n,m) = map(int,input().split())
    if(abs(n-m)>1):
        return 0
    if(n==m):
        return (factorial(n)**2 * 2) % 1000000007
    if(n > m):
        return (factorial(m)**2 * n) % 1000000007
    else:
        return (factorial(n)**2 * m) % 1000000007

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

Submission status: Wrong Answer

Solution code:
#Reconciled?
import numpy as np
import math as mt
N,M = list(map(int, input().split()))
if(abs(N-M)>1):
    a=0
else:
    if (N==M):
        a = 2*2*mt.factorial(N)
    if (N!=M):
        a = mt.factorial(max([N,M]))*mt.factorial(min([N,M]))
print(a%(10**9+7))

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
for i in range(2):
	a[i] = int(a[i])
seki1 = 1
seki2 = 1
if (a[0] - a[1] == 1 or a[0] -a[1] == -1):
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % (10**9+7)
	for i in range(a[1]):
		seki2 *= i+1
		seki2  = seki1 % (10**9+7)
	ans = (seki1*seki2) % (10**9 + 7)
	print ans
	
elif a[0] == a[1]:
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % (10**9+7)

	ans = (2*seki1*seki1) % (10**9 + 7)
	print ans
	
else:
	print 0

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
for i in range(2):
	a[i] = int(a[i])
seki1 = 1
seki2 = 1
if (a[0] - a[1] == 1 or a[0] -a[1] == -1):
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % 10**9+7
	for i in range(a[1]):
		seki2 *= i+1
		seki2  = seki1 % 1e9+7
	ans = (seki1*seki2) % (10**9 + 7)
	print ans
	
elif a[0] == a[1]:
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % 10**9+7
	ans = (2*seki1*seki1) % (10**9 + 7)
	print ans
	
else:
	print 0

Submission status: Wrong Answer

Solution code:
def reconclied():

	N, M = [int(i) for i in input().split()]

	R = 1000000007

	if N - M < -1 or N - M > 1:
		print(0)
		return
	
	if N == M:
		n = N
		while True:
			if n == 1:
				print(((N**2)*2)%R)
				return
			N *= (n-1)
			if N >= R:
				N = N % R
			n -= 1
	
	if M > N:
		M, N = N, M
	
	n = N - 2
	NN = N
	N = N-1
	while True:
		if n == 1:
			M = N
			N *= NN
			print((N*M)%R)
			return
		N *= (n-1)
		if N >= R:
			N = N % R
		n -= 1
	
			
		
reconclied()

Submission status: Wrong Answer

Solution code:
import math
ans = 0
n,m = map(int,input().split())
n = math.factorial(n)
m = math.factorial(m)
if abs(n - m) < 2:
    if n == m:
        ans = 2*n*n
        print(ans % (10**9 + 7))
    else:
        ans = 2 * n * m
        print(ans % (10**9 + 7))
else:
    print("0")

Submission status: Wrong Answer

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

        ans = n * m
        print(ans % (10**9 + 7))
else:
    print("0")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N=input()
N,M=N.split()
N=int(N)
M=int(M)
Nk=1
Mk=1

if math.fabs(N-M)>1:
    print(0)
    quit()


for i in range(1,N+1):
    Nk=Nk*i%(1000000000+7)
for i in range(1,M+1):
    Mk=Mk*i%(1000000000+7)

if N!=M:
    print(Nk*Mk%(1000000000+7))
else:
    res=Nk*Mk*(N)
    print(res%(1000000000+7))


quit()

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
if N - M >= 2 or M - N >= 2:
	print(0)
else:
	if N == M:
		n = 1
		for i in range(N):
			n = (n * (i + 1)) % 1000000007
		n = (n ** 2) % 1000000007
		n = (2 * n) % 1000000007
		print(n)

	elif N - M == 1:
		n = 1
		for i in range(M):
			n = (n * (i + 1)) % 100000007
		n = (n ** 2) % 1000000007
		n = (n * N) % 1000000007
		print(n)
	else:
		n = 1
		for i in range(N):
			n = (n * (i + 1)) % 100000007
		n = (n ** 2) % 1000000007
		n = (n * M) % 1000000007
		print(n)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
a = 10**9 + 7
def fact(n):
    f = 1
    for i in range(1,1+n):
        f *= i
    return f
if(abs(N-M)>1):print(0)
elif(N != M):
    minc = fact(min(N,M))
    maxc = fact(max(N,M))
    print(int((minc*maxc)%a))
elif(N == M):
    minc = fact(N)
    maxc = fact(N-1)*2
    print(int((minc*maxc*2)%a))

Submission status: Wrong Answer

Solution code:
import math
N=input()
N,M=N.split()
N=int(N)
M=int(M)
Nk=1
Mk=1

if math.fabs(N-M)>1:
    print(0)
    quit()


for i in range(1,N+1):
    Nk=Nk*i
for i in range(1,M+1):
    Mk=Mk*i

if N!=M:
    print(Nk*Mk%(1000000000+7))
else:
    res=Nk*Mk*(N)
    print(res%(1000000000+7))


quit()

Submission status: Wrong Answer

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

if abs(m-n)>1:
  print(0)

elif m==n:
  print((m*n*2)%(10**9+7))

else:
  print((max(m,n)*(max(m,n)-1)*min(m,n))%(10**9+7))

Submission status: Wrong Answer

Solution code:
import sys

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

if abs(n - m) > 1:
    print(0)
    sys.exit()

a = n

for i in range(2,n):
    a = (a * i) % 1000000007

if n == m:
    print((a * a * 2) % 1000000007)
elif n < m:
    print((a * a * m) % 1000000007)
else:
    print((a * a // n) % 1000000007)

Submission status: Wrong Answer

Solution code:
import sys

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

if abs(n - m) > 1:
    print(0)
    sys.exit()

a = n

for i in range(2,n):
    a = (a * i) % 1000000007

if n == m:
    print((a * a * 2) % 1000000007)
elif n < m:
    print((a * a * m) % 1000000007)
else:
    print((a * a / m) % 1000000007)

Submission status: Wrong Answer

Solution code:
import math
def ini(): return int(input())
def inli(): return list(map(int, input().split()))
def inf(): return float(input())
def inlf(): return list(map(float, input().split()))
def inl(): return list(input())
def pli(): return "".join(list(map(str, ans)))


a = inli()
n=a[0]
m=a[1]
if n-m ==1 or m-n==1:
    if n < m:
        tmp =math.factorial(n)
        print(tmp * tmp * m)
    else:
       tmp = math.factorial(m)
       print(tmp * tmp *n)

elif n==m :
    tmp =math.factorial(m)
    print(tmp * tmp)
else :
    print(0)

Submission status: Wrong Answer

Solution code:
max = 10 ** 9 + 7
N, M = map((int), input().split() )
if(abs(N - M) > 1):
    print(0)
    exit()
dog = 1
monkey = 1
while(True):
    dog *= N
    monkey *= M
    if(N > 0):
        N -= 1
    if(M > 0):
        M -= 1
    if(N <= 0 and M <= 0):
        break
if(N == M):
    print((2 * dog * monkey) % max)
else:
    print((dog * monkey) % max)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

def mul(a,b):
    return (a*b) % MOD

N,M = map(int, raw_input().split())
A = max(N,M)
B = min(N,M)

def solve():
    if A-B > 2: return 0
    factorical = [1]
    for n in range (1,A+1):
        f = mul(factorical[n-1], n)
        factorical.append(f)
    if A == B:
        return mul(2, mul(factorical[A], factorical[B]))
    else:
        return mul(factorical[A], factorical[B])

print solve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !

n,m=input().split()

l=[1]
for i in range(1,100001):
    l.append(l[i-1]*(i)%1000000007)
n=int(n)
m=int(m)

if n==m:
    print(l[n]*l[m]*2%1000000007)
elif (n-m==1):
    print(l[n]*l[m]%1000000007)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import sys
import math
memo = {1:1}
def factorial(n):
    for i in range(len(memo)+1,n+1):
        if n not in memo:
            memo[i] = int((memo[i-1] * i) % (1e9+7))
    return memo[n]

N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
    sys.exit()
elif N == M:
    ans = factorial(N) * factorial(M) * 2
else:
    ans = factorial(N) * factorial(M)
ans = int(ans % (1e9+7))
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !

n,m=input().split()

l=[1]
for i in range(1,100001):
    l.append(l[i-1]*(i)%1000000007)
n=int(n)
m=int(m)

if n==m:
    print(l[n]*l[m]*2%1000000007)
elif (n-m==1):
    print(l[n]*l[m])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math

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

if n > m:
    m, n = n, m

if m - n > 2:
    print("0")
elif m - n == 1:
    print((math.factorial(m) * math.factorial(n)) % 1000000007)
elif m - n == 0:
    print((math.factorial(m) * math.factorial(n) * 2) % 1000000007)

Submission status: Wrong Answer

Solution code:
import math

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

if math.fabs(N-M)>2:
    print(0)
elif N==M:
    print((math.factorial(N)*math.factorial(M)*2)%(10**9+7))
else:
    print((math.factorial(N)*math.factorial(M))%(10**9+7))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if m>=n*2 or n>=2*m:exit(print(0))
f1,f2,mod=1,1,10**9+7
for i in range(1,n+1):
  f1=(f1*i)%mod
for i in range(1,m+1):
  f2=(f2*i)%mod
ans=(f1*f2)%mod
if n==m: ans=(ans*2)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if m>n*2 or n>2*m:exit(print(0))
f1,f2,mod=1,1,10**9+7
for i in range(1,n+1):
  f1=(f1*i)%mod
for i in range(1,m+1):
  f2=(f2*i)%mod
ans=(f1*f2)%mod
if n==m: ans=(ans*2)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import math 

N,M=map(int,raw_input().split(' '))

if N==M:
    print (2*math.factorial(N)*math.factorial(M))%(10**9+7)
elif abs(N-M)==1:
    print (math.factorial(N)*math.factorial(M))%(10**9+7)
elif abs(N-M)>2:
    print 0

Submission status: Time Limit Exceeded

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



def fac(n):
  x = 1
  for i in range(1,n+1):
    x *= i
  return x

nf = fac(n) % p
mf = fac(m) % p

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

elif n == m:
  ans = nf * mf * 2
  
else:
  ans = nf * mf
  
print(ans % p)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
a,b=map(int, input().split())

if abs(a-b) == 1:
    print(math.factorial(a)*math.factorial(b))
if abs(a-b) == 0:
    print(math.factorial(a)*math.factorial(b)*2)
if abs(a-b) >= 2:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

N, M = map(int, input().split())
MOD = 10**9 + 7

if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    ret = 1
    for i in range(1, N+1):
        ret *= i % MOD
    for i in range(1, M+1):
        ret *= i % MOD

    print(ret%MOD)
else:
    ret = 1
    for i in range(1, N+1):
        ret *= i % MOD
    for i in range(1, M+1):
        ret *= i % MOD

    print(2*ret%MOD)

Submission status: Time Limit Exceeded

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

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

ans = 0
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = S*S*max(N,M)
else:
    ans = S*S*2

ans = ans % 1000000007

print(ans)

Submission status: Time Limit Exceeded

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

S = 1
for i in range(1,min(N,M)+1):
    S *= i

ans = 0
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = S*S*max(N,M)
else:
    ans = S*S*2

ans = ans % 1000000007

print(ans)

Submission status: Time Limit Exceeded

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

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

Sm = 1
for i in range(1,M+1):
    Sm *= i

ans = 0
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = Sn * Sm
else:
    ans = Sn*Sm*2

ans = ans % 1000000007

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = sorted([int(x) for x in input().split()])
gap = m - n
if gap > 1:
    print(0)
    exit()
mod = 10 ** 9 + 7
f = 1
for i in range(1, m + 1):
    f *= i
    if i == n:
        f_n = f % mod
f_m = f % mod
ans = f_n * f_m % mod
if gap == 0:
    ans = ans * 2 % mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = sorted([int(x) for x in input().split()])
gap = m - n
if gap > 1:
    print(0)
    exit()
mod = 10 ** 9 + 7
f = f_n = 1
for i in range(2, m + 1):
    f *= i
    if i == n:
        f_n = f % mod
f_m = f % mod
ans = f_n * f_m % mod
if gap == 0:
    ans = ans * 2 % mod
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    ans=(fact(n)*fact(m))%mod
    if n==m: print((2*ans)%mod)
    else: print(ans)

Submission status: Time Limit Exceeded

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

mod =(10**9) + 7
v_n = 1
for i in range(1,n+1):
    v_n*= i%mod
v_m = 1
for j in range(1,m+1):
    v_m*= j%mod

if n==m:
    value = (v_n *v_m * 2)%mod
    print(value)

elif abs(n-m)>2:
    print(0)
    
else:
    ans = (v_n * v_m)%mod
    print(ans)

Submission status: Time Limit Exceeded

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

mod =pow(10,9) + 7
from operator import mul
from functools import reduce

def factrial(n):
    over = reduce(mul, range(n, 0, -1))
    return over

if n==m:
    value = (factrial(n) * factrial(m) * 2 )%mod

    print(value)

elif abs(n-m)>2:
    print(0)
else:
    ans = (factrial(n) * factrial(m))%mod

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
m,n = list(map(int, input().split()))
MOD=10**9+7
if abs(m-n) > 1:
    print(0)
    exit()

def fact(x):
    ans=1
    for i in range(2, x+1):
        ans*=i%MOD
    return ans

if m>n:
    n,m = m,n
    
f = fact(m)
if m==n:
    print(f * f * 2 % MOD)
else:
    print(f * f * n % MOD)

Submission status: Time Limit Exceeded

Solution code:
m,n = list(map(int, input().split()))
MOD=10**9+7
if abs(m-n) > 1:
    print(0)
    exit()

def fact(x, mod):
    ans=1
    for i in range(2, x+1):
        ans*=i%mod
    return ans

if m>n:
    n,m = m,n
    
f = fact(m,MOD)
if m==n:
    print(f * f * 2 % MOD)
else:
    print(f * f * n % MOD)

Submission status: Time Limit Exceeded

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

mod = 10**9 + 7

if abs(n-m) > 1:
    print(0)
    quit()

elif abs(n-m) == 1:
    a = max(n,m)
    b = min(n,m)
    ans = 1
    for i in range(1,a+1):
        ans *= (i%mod)

    for i in range(1,b+1):
        ans *= (i%mod)
    print(ans%mod)

else:
    ans = 1
    for i in range(1,n+1):
        ans *= (i%mod)
    for i in range(1,n+1):
        ans *= (i%mod)
    print((2*ans)%mod)

Submission status: Time Limit Exceeded

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

INF = 10**9 + 7

def frac(n):
    ans = n
    while n>1:
        ans *= (n-1)
        n -= 1
    return ans
    # if n<= 1:
    #     return 1
    # return n*frac(n-1)
    
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    print(frac(N)*frac(M)%INF)
else:
    print(((frac(N)*frac(M))*2)%INF)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    print(math.factorial(N)*math.factorial(M))
else:
    print((2*math.factorial(N)*math.factorial(M))%(10**9+7))

Submission status: Time Limit Exceeded

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


elif abs(n-m) == 1:
    for i in range(1,min(n+1,m+1)):
        ans *= i
    ans = max(n,m)*(ans**2)
    print(ans%(10**9+7))


else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
MOD=10**9+7
s=1
for i in range(2,N+1):
  s=s*i
t=1
for j in range(2,M+1):
  t=t*j
if abs(N-M)>=2:
  print(0)
if abs(N-M)==1:
  print((s*t)%MOD)
if N==M:
  print((2*s*t)%MOD)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
MOD = 1000000007
ans = 1
if abs(n - m) == 1:
    for i in range(1, min(n, m) + 1):
        ans *= (i ** 2) % MOD
    ans = (ans * max(n, m)) % MOD
elif n == m:
    for i in range(1, min(n, m) + 1):
        ans *= (i ** 2) % MOD
    ans =  (ans * 2) % MOD
else:
    ans = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
n,m = map(int,input().split())
if abs(n-m) >= 2:
    num_dm = 0
elif abs(n-m) == 1:
    num_dm = 1
else:
    num_dm = 2
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
d_comb_num = factorial(n)
m_comb_num = factorial(m)
ans = num_dm * d_comb_num * m_comb_num
print(ans)