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

Solution (Python):
from math import factorial as f
n,m=map(int,input().split())
if abs(n-m) >= 2:
  print(0)
  quit()
if n == m:
  print(f(n)*f(m)*2%(10**9+7))
else:
  print(f(max(m,n))*f(min(m,n))%(10**9+7))

Solution (Python):
N, M = map(int, input().split())
mod = 10**9 + 7

def factorial(n):
    ans = 1
    i = 0
    while i < n:
        ans *= (n-i)
        ans %= mod
        i += 1
    return ans

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

Solution (Python):

"""
Created on Sun Sep 27 02:58:21 2020

@author: liang
"""

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

if abs(N-M) >= 2:
    ans = 0
else:
    ans = 1
    for i in range(1,N+1):
        ans *= i
        ans %= MOD

    for i in range(1,M+1):
        ans *= i
        ans %= MOD
    
    if N == M:
        ans *= 2
        ans %= MOD
print(ans)

Solution (Python):
n,m = map(int, input().split())
if n > m:
    n,m = m,n
ans = 1
if m==n:

    while n > 0:
        ans *= n
        ans %= 10**9+7
        n -= 1
    ans = (2*(ans**2))%(10**9+7)
    print(ans)

elif m - n == 1:
    while n > 0:
        ans *= n
        ans %= 10**9+7
        n -= 1
    
    
    ans = (ans * (ans*m % (10**9+7))) % (10**9+7)
    print(ans)
else:
    print(0)

Solution (Python):
def factorial(x, mod=10**9+7):
    v = 1
    for i in range(2, x+1):
        v *= i
        v %= mod
    return v


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

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

mod = 10**9 + 7
if N == M:
    ans = (factorial(N) * factorial(M) * 2) % mod
else:
    ans = (factorial(N) * factorial(M)) % mod

print(ans)

Solution (Python):
MOD =  pow(10,9)+7
def MODINV(n:int, MOD=MOD):
    return pow(n, MOD-2, MOD)

def perm(n, k, MOD):
    numer = 1
    for i in range(n, n-k, -1):
        numer *= i
        numer %= MOD
    return numer

def main():
    N, M = map(int, input().split())
    if N == M+1 or N+1 == M:
        ans = (perm(N, N, MOD)*perm(M, M, MOD))%MOD
    elif N == M:
        ans = (2*(perm(N, N, MOD)*perm(M, M, MOD)))%MOD
    else:
        ans = 0
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from math import factorial as fac

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

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

Solution (Python):
n,m = map(int, input().split())
f = [1] 
p = 10**9 + 7
for i in range(1, max(n,m) + 1):
  f.append(f[-1] * i % p)

if abs(n-m) == 1:
  print(f[n] * f[m] % p)
elif abs(n-m) == 0:
  print(f[n] * f[m] * 2 % p)
else:
  print(0)

Solution (Python):
MOD = 10 ** 9 + 7

kai = [1, 1]
for i in range(2, 10 ** 5 + 1):
    num = kai[-1] * i
    kai.append(num % MOD)
n, m = map(int, input().split())
if abs(n - m) > 1:
    ans = 0
else:
    ans = kai[n] * kai[m]
    if n == m:
        ans *= 2
    ans %= MOD
print(ans)

Solution (Python):
import math
n,m = map(int,input().split())
mod = 10**9+7
fact_n = math.factorial(n) % mod
fact_m = math.factorial(m) % mod

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

Solution (Python):
import math
n,m = map(int,input().split())
mod = 10**9+7
fact_n = math.factorial(n) % mod
fact_m = math.factorial(m) % mod

if abs(n-m) >= 2:
  print(0)
  exit()
elif n == m:
  print((fact_n * fact_m * 2) % mod)
else:
  print((fact_n * fact_m) % mod)

Solution (Python):

def main():
    from math import factorial

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

    if abs(n-m) < 2:
        if n == m:
            print(2*factorial(n)*factorial(m)%p)
        else:
            print(factorial(n)*factorial(m)%p)
    else:
        print(0)

if __name__ ==  "__main__":
    main()

Solution (Python):
N, M = map(int, input().split())
mod = 1000000007

if abs(N - M) > 1:
    print(0)
elif (N + M) % 2 == 1:
    x = max(N, M)
    y = min(N, M)
    Sum = 1
    for i in range(N + M):
        if i % 2 == 0:
            Sum *= x - (i // 2)
            Sum %= mod
        else:
            Sum *= y - (i // 2)
            Sum %= mod
    print(Sum)
else:
    Sum = 1
    for i in range(2 * N):
        Sum *= N - (i // 2)
        Sum %= mod
    Sum *= 2
    Sum %= mod
    print(Sum)

Solution (Python):
N, M = map(int, input().split())
from math import factorial
mod = 10**9+7

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

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


def facmod(a, mod):
    res = 1
    for i in range(1, a+1):
        res = (res*i) % mod
    return res


mod = 10**9+7
mn = min(m, n)
mx = max(m, n)
if abs(mn-mx) > 1:
    ans = 0
else:
    if n == m:
        ans = (2*facmod(m, mod)*facmod(n, mod)) % mod
    else:
        ans = (facmod(mx, mod)*facmod(mn, mod)) % mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline




'''
a,b=[],[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''

n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)
    sys.exit()

f=[1]
for i in range(1,max(n,m)+1):
    x=f[i-1]*i
    if x>10**9+7:
        x%=10**9+7
    f.append(x)
if n!=m:
    print(f[n]*f[m]%(10**9+7))
else:
    print(f[n]*f[m]*2%(10**9+7))

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



nf = math.factorial(n) % p
mf = math.factorial(m) % p

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

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

Solution (Python):
import math
N,M = map(int,input().split())
f = 1
if abs(N-M) > 1:
    print(0)
    exit()
if (N+M) % 2 == 0:
    f = 2

print(math.factorial(N) * math.factorial(M) * f % (10**9+7))

Solution (Python):
import math
N,M = map(int,input().split())
f = 1
if abs(N-M) > 1:
    print(0)
    exit()
if (N+M) % 2 == 0:
    f = 2

print(math.factorial(N) % (10**9+7) * math.factorial(M) % (10**9+7) * f % (10**9+7))

Solution (Python):
N, M = map(int, input().split())
NUM = 10**9 + 7


if N-1 <= M <= N+1:
    ans = 1
    if N == M:
        for _ in range(2):
            for i in range(1, N+1):
                ans = ans * i % NUM
        ans = ans * 2 % NUM
    else:
        for i in range(1, N+1):
            ans = ans * i % NUM
        for i in range(1, M+1):
            ans = ans * i % NUM
else:
    ans = 0

print(ans)

Solution (Python):
def factorial(n, mod):
    num = 1
    for i in range(1, n + 1):
        i %= mod
        num *= i
        num %= mod
    return num


def main():
    n, m = map(int, input().split())
    md = 10 ** 9 + 7

    if abs(n - m) >= 2:
        ans = 0
    elif abs(n - m) == 1:
        ans = (factorial(n, md) * factorial(m, md))
        ans %= md
    else:
        ans = (factorial(n, md) * factorial(m, md)) * 2
        ans %= md

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float('inf')

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float('inf')
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float('inf')
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(' ')
        elif type(ls[0])==str:
            print(' '.join(ls))
        elif type(ls[0])==int:
            print(' '.join(map(str,ls)))
        else:
            print('Output Error')
    else:
        if len(ls)==0:
            print('')
        elif type(ls[0])==str:
            print(''.join(ls))
        elif type(ls[0])==int:
            print(''.join(map(str,ls)))
        else:
            print('Output Error')

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]







mod = 10**9 + 7

n,m = MI()

a = factorial(n)%mod
b = factorial(m)%mod

if n==m: 
    ans = a*b*2 % mod
elif (n+1)==m or (m+1)==n:
    ans = a*b % mod
else:
    ans = 0

print(ans)

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

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

Solution (Python):
def main():
    N,M = map(int,input().split())
    if abs(N-M) > 1:
        return 0
    N_mlt = 1
    M_mlt = 1
    for i in range(1,N+1,1):
        N_mlt *= i
        N_mlt = N_mlt%1000000007
        
    for i in range(1,M+1,1):
        M_mlt *= i
        M_mlt = M_mlt%1000000007
        
    
    ans = (N_mlt*M_mlt)%1000000007
    if abs(N-M)==0:
        ans = (ans*2)%1000000007
        return  ans
    else:
        return ans

print(main())

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

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

p = 10 ** 9 + 7
n = 10 ** 5  
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  
for i in range(2, n + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if N==M:
    ans=2*fact[N]*fact[M] % p
else:
    big = max(N, M)
    small = min(N, M)
    ans = fact[N]*fact[M] % p

print(ans)

Solution (Python):
MOD = 10**9+7

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

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


dog = 1
monkey = 1
for i in range(1,N+1):
    dog = (dog*i)%MOD
for j in range(1,M+1):
    monkey = (monkey*j)%MOD

ans =(dog*monkey)%MOD
if N==M:
    ans=(ans*2)%MOD

print(ans)

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


def permitation(x):
    res = 1
    for i in range(1, x + 1):
        res = res * i % mod
    return res


pn = permitation(n)
pm = permitation(m)

if n == m:
    res = pn * pm * 2 % mod
    print(res)
elif n == m - 1 or n == m + 1:
    res = pn * pm % mod
    print(res)
else:
    print(0)

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

Solution (Python):

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

if abs(N-M) >= 2:
    ans = 0
else:
    
    Mfact = 1
    for m in range(1,M+1):
        Mfact = (Mfact * m)%MOD
    Nfact = 1
    for n in range(1,N+1):
        Nfact = (Nfact * n)%MOD
    ans = (Nfact*Mfact)%MOD
    if abs(N-M) == 0:
        ans = (ans * 2)%MOD
print(ans)

Solution (Python):
import math

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

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

elif abs(n - m) == 1:
    ans = math.factorial(n) * math.factorial(m)
    ans %= 10 ** 9 + 7
    print(ans)

else:
    ans = 2*math.factorial(n) * math.factorial(m)
    ans %= 10 ** 9 + 7
    print(ans)

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

ans = 0
rd = 10**9 + 7
if(abs(n-m) > 1):
    ans = 0
    print(ans)
elif(abs(n-m) == 1):
    tmp = min(n,m)
    tmp_mx = max(n,m)
    sm = 1
    for i in range(tmp):
        sm = (sm * (i+1))%rd
    
    ans = (sm * sm * tmp_mx)%rd
    print(ans)
else:
    sm = 1
    for i in range(n):
        sm = (sm * (i+1))%rd
    
    ans = (2*sm * sm)%rd
    print(ans)

Solution (Python):
import math

N, M = map(int, input().split())
if abs(N - M) >= 2:
    print(0)
    exit()


N = math.factorial(N) % (10 ** 9 + 7)
M = math.factorial(M) % (10 ** 9 + 7)
if N == M:
    print((N * M * 2) % (10 ** 9 + 7))
else:
    print((N * M) % (10 ** 9 + 7))

Solution (Python):
N, M = [int(x) for x in input().split()]
MOD = 10**9+7

if max(N, M) - min(N, M) > 1:
    print(0)
    exit()


def floor(n):
    result = n
    for i in range(n-1, 0, -1):
        result *= i
        result %= MOD

    return result


r = floor(max(N, M))
t = floor(min(N, M))

if r == t:
    result = (r * t * 2) % MOD
else:
    result = (r * t) % MOD

print(result)

Solution (Python):
import sys
import math

mod = 10**9 + 7
N, M = map(int, sys.stdin.readline().split())
diff = abs(N - M)
if diff > 1:
    print(0)
    sys.exit()

n_p = 1
for i in range(1, N+1):
    n_p *= i
    n_p %= mod
m_p = 1
for i in range(1, M+1):
    m_p *= i
    m_p %= mod

if diff == 0:
    print(2 * n_p * m_p % mod)
else:
    print(n_p * m_p % mod)

Solution (Python):
import sys


def LI():
    return list(map(int, input().split()))


N, M = LI()
if abs(N-M) >= 2:
    print(0)
    sys.exit()
mod = pow(10, 9)+7
x = 1
if N == M:
    for i in range(1, N+1):
        x = (x*i) % mod
    ans = (x*x*2) % mod
else:
    A = max(N, M)
    B = min(N, M)
    for i in range(1, B+1):
        x = (x*i) % mod
    y = (x*A) % mod
    ans = (x*y) % mod
print(ans)

Solution (Python):
p = 10**9+7
N,M = map(int,input().split())
A = [1 for _ in range(N+1)]
for i in range(2,N+1):
    A[i] = (i*A[i-1])%p
B = [1 for _ in range(M+1)]
for i in range(2,M+1):
    B[i] = (i*B[i-1])%p
if abs(N-M)>1:
    ans = 0
elif abs(N-M)==1:
    ans = (A[N]*B[M])%p
elif abs(N-M)==0:
    ans = (2*A[N]*B[M])%p
print(ans)

Solution (Python):
def perm(n,r,p):
  if (r < 0) or (n < r):
    return 0
  return fac[n]*finv[n-r]%p

n = pow(10,5)*2+10
MOD = pow(10,9)+7

fac = [-1]*(n+1); fac[0] = 1; fac[1] = 1 
finv = [-1]*(n+1); finv[0] = 1; finv[1] = 1 
inv = [-1]*(n+1); inv[0] = 0; inv[1] = 1 
for i in range(2,n+1):
  fac[i] = fac[i-1]*i%MOD
  inv[i] = MOD - inv[MOD%i]*(MOD//i)%MOD
  finv[i] = finv[i-1]*inv[i]%MOD


N,M = map(int,input().split())
if abs(N-M) > 1:
  print(0);exit()
if N == M:
  ans = fac[N]*perm(M,N-1,MOD)*2
else:
  if M > N:
    N,M = M,N
  ans = fac[N]*perm(M,N-1,MOD)
print(ans%MOD)

Solution (Python):
n,m=map(int,input().split())
p=10**9+7
n,m=min(n,m),max(n,m)
if m-n>=2:
    print(0)
elif m-n==1:
    ans=1
    for i in range(m):
        ans=ans*(i+1)%p
    for i in range(n):
        ans=ans*(i+1)%p
    print(ans%p)
else:
    ans=1
    for i in range(n):
        ans=ans*(i+1)%p
    print(2*ans*ans%p)

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

Solution (Python):
mod = 10 ** 9 + 7

def factorial(n, mod):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
        fact %= mod
    return fact

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

Solution (Python):
import sys

mod = 10**9 + 7
n = 10**6
 
fact = [1]*(n+1)
rfact = [1]*(n+1)
r = 1
for i in range(1, n+1):
    fact[i] = r = r * i % mod
rfact[n] = r = pow(fact[n], mod-2, mod)
for i in range(n, 0, -1):
    rfact[i-1] = r = r * i % mod


def perm(n, k):
    return fact[n] * rfact[n-k] % mod
 

def comb(n, k):
    return fact[n] * rfact[k] * rfact[n-k] % mod

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

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

print(2*(perm(n,n)*perm(m,m))%mod) if n == m else print((perm(n,n)*perm(m,m))%mod)

Solution (Python):
MOD=1000000007

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

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

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

Solution (Python):
def main():
    N, M = map(int, input().split())
    if abs(N - M) > 1:
        print(0)
        exit()
    v = 10 ** 9 + 7
    ans = 1
    for n in range(1, N+1):
        ans = (ans * n) % v
    for m in range(1, M+1):
        ans = (ans * m) % v
    if N == M:
        ans = (ans * 2) % v
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,m = readInts()
mod = 10**9+7
if abs(n-m)>1:
	print(0)
else:
	ans = 1
	for i in range(1,n+1,):
		ans = (ans*(i%mod))%mod
	for i in range(1,m+1):
		ans = (ans*(i%mod))%mod
	if abs(n-m)==0:
		ans = (ans*2)%mod
	print(ans)

Solution (Python):
import math
N, M = map(int, input().split())

mod = 10 ** 9 + 7

if abs(N - M) > 1:
  ans = 0
elif abs(N - M) == 1:
  ans = (math.factorial(N) % mod) * (math.factorial(M) % mod)
else:
  ans = (math.factorial(N) % mod) * (math.factorial(M) % mod) * 2
  
print(ans % mod)

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

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

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

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    N,M = i_map()
    s = abs(N-M)
    z = 10**9 + 7

    if s >= 2:
        print(0)
        exit()

    if s == 0:
        a = math.factorial(N)
        print(a**2*2%z)
    else:
        a = math.factorial(N)
        b = math.factorial(M)
        print(a*b%z)

if __name__=="__main__":
    main()

Solution (Python):
with open(0) as f:
    N, M = map(int, f.read().split())
mod = 1000000007

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

if abs(N-M) > 1:
    ans = 0
elif N == M:
    ans = 2*factorial(M)**2 %mod 
else:
    N, M = max(N, M), min(N, M)
    ans = N * factorial(M)**2 %mod
print(ans)

Solution (Python):
MOD = 1_000_000_007
n, m = map(int,input().split())
if abs(n-m)>1:
    print(0)
else:
    if abs(n-m)==1:
        ans = 1
    else:
        ans = 2
    for i in range(n):
        ans = (ans*(i+1))%MOD
    for i in range(m):
        ans = (ans*(i+1))%MOD
    print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N,M = mint()
if abs(N-M)>1:
    print(0)
else:
    ans = perm(N,MOD)*perm(M,MOD)*(2-abs(N-M))
    print(ans%MOD)

Solution (Python):
def gyakugen(x,p):
  binp = str(bin(p-2))[::-1]
  ruijou = x
  
  result = 1
  for i in range(len(binp)-2):
    if binp[i] == "1":
      result *= ruijou
      result %= p
    ruijou *= ruijou
    ruijou %= mod
  return result

N, M = [int(i) for i in input().split()]
mod = 10**9+7

if abs(N-M) > 1 :
  print(0)
  exit()
elif N == M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= mod
    ans *= i
    ans %= mod
  ans *= 2
  ans %= mod
  print(ans)
else:
  ans = 1
  m = min(N,M)
  for i in range(1,m+1):
    ans *= i
    ans %= mod
    ans *= i
    ans %= mod
  ans *= max(N,M)
  ans %= mod
  print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000)

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

def kaijo(n):
    if n == 1:
        return 1
    return n * kaijo(n - 1) % ((10 ** 9) + 7)


if abs(n - m) > 1:
    print(0)
    exit()
a = kaijo(n)
b = kaijo(m)

if n == m:
    print((a * b * 2) % ((10 ** 9) + 7))
else:
    print((a * b ) % ((10 ** 9) + 7))

Solution (Python):
n, m = map(int, input().split())
n, m = max(n, m), min(n, m)
div = 10 ** 9 + 7
res = 1
if n - m == 0:
  for i in range(1, n + 1):
    res *= i
    res %= div
  res = res * res * 2
  print(res % div)
elif n - m == 1:
  for i in range(1, n + 1):
    res *= i
    res %= div
  for i in range(1, m + 1):
    res *= i
    res %= div
  print(res)
else:
  print(0)

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

Solution (Python):
N,M=map(int,input().split())
MAX=10**5+10
MOD = 10**9 + 7
def COMinit():
    """
    逆元テーブルをつくる前処理
    計算量はO(n)
    1 <= k <= n <= 1e7程度
    p: 素数
    """
    fac = [0]*MAX
    finv = [0]*MAX
    inv = [0]*MAX

    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[0], inv[1] = 1, 1
    for i in range(2, MAX):
        fac[i] = fac[i-1] * i % MOD
        inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
        finv[i] = finv[i-1] * inv[i] % MOD
    return fac, finv, inv

fac, finv, inv = COMinit()
if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    ans = fac[N]*fac[M]%MOD
    print(ans)
else:
    ans = 2*fac[N]*fac[M]%MOD
    print(ans)

Solution (Python):
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)%(10**9+7))
else:
  print(math.factorial(n)*math.factorial(m)*2%(10**9+7))

Solution (Python):
N,M=map(int,input().split())
if N==M+1:
    A=N
    B=M
    ans=1
elif N+1==M:
    A=M
    B=N
    ans=1
elif N==M:
    A=N
    B=M
    ans=2
else:
    print(0)
    exit()
ansa=1
ansb=1
for i in range(1,A+1):
    ansa=(ansa*i)%(10**9+7)
for j in range(1,B+1):
    ansb=(ansb*j)%(10**9+7)

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

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

mod = 10**9+7
ans = 1


if abs(n-m) >= 2:
    ans = 0
elif abs(n-m) == 1:
    for i in range(1, max(n, m)+1):
        ans *= i
        ans %= mod
    for j in range(1, min(n, m)+1):
        ans *= j
        ans %= mod
elif abs(n-m) == 0:
    for i in range(1, n+1):
        ans *= i
        ans %= mod
    ans **= 2
    ans*= 2
    ans %= mod


print(ans)

Solution (Python):
n,m=[int(i) for i in input().split()]
new_n=min(n,m)
new_m=max(n,m)
mod=10**9+7
def ans(n,m):

    if n==m:
        return 2*kaijyou(n,mod)*kaijyou(m,mod)
    elif m-n==1:
        return kaijyou(n,mod)*kaijyou(m,mod)
    else:
        return 0


def kaijyou(n,mod):
    ans=1
    for i in range(1,n+1):
        ans*=i
        ans=ans%mod
    return ans
print(ans(new_n,new_m)%mod)

Solution (Python):



import sys
from io import StringIO
import unittest
import math


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2 2"""
        output = """8"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 2"""
        output = """12"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1 8"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """100000 100000"""
        output = """530123477"""
        self.assertIO(input, output)


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

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


if __name__ == "__main__":
    
    resolve()

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
MOD = 10 ** 9 + 7
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def fact_mod(n, MOD):
    ret = 1
    for i in range(1, n + 1):
        ret *= i
        ret %= MOD
    return ret

def resolve():
    N, M = LI()

    if N == M:
        ans = 2 * pow(fact_mod(N, MOD), 2, MOD)
        ans %= MOD
        print(ans)
    elif abs(N - M) <= 1:
        ans = fact_mod(N, MOD) * fact_mod(M, MOD)
        ans %= MOD
        print(ans)
    else:
        print(0)


if __name__ == '__main__':
    resolve()

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

Solution (Python):
N,M=map(int,input().split())
t=10**9+7
import math
n=math.factorial(N)%t
m=math.factorial(M)%t

if 2<=abs(N-M):
    print(0)
elif abs(N-M)==1:
    print((n*m)%t)
else:
    print((n*m*2)%t)

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

if n < m: 
    n, m = m, n

if m + 1 == n or m == n:
    
    if m + 1 == n:
        ans = 1
    else:
        ans = 2
    k = 1
    for i in range(1, n + 1):
        k *= i
        if i == m:
            ans *= k
        k = k % (10**9 + 7)
    ans *= k
    print(ans % (10**9 + 7))
else:
    print(0)

Solution (Python):
import math
n,m=map(int,input().split())
N=math.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))

Solution (Python):
class Factorials:
    
    def __init__(self, n, mod):
        self.mod = mod

        
        self.fac = [1]
        num = 1
        for i in range(1, n+1):
            num *= i
            num %= mod
            self.fac.append(num)

        
        num = pow(num, mod-2, mod)
        self.rec = [1 for i in range(n+1)]
        self.rec[n] = num
        for i in range(n-1, 0, -1):
            num *= i + 1
            num %= mod
            self.rec[i] = num

    
    def comb(self, n, r):
        return self.fac[n] * self.rec[r] * self.rec[n - r] % self.mod
    
    
    def perm(self, n, r):
        return self.fac[n] * self.rec[n-r] % self.mod

    
    def fact(self, n):
        return self.fac[n]

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

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

fct = Factorials(n + 1, mod)

if abs(n - m) == 1:
    print(fct.fact(n) * fct.fact(m) % mod)

else:
    print(2 * fct.fact(n) * fct.fact(m) % mod)

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