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):
import sys
import math

input = sys.stdin.readline


def main():
    N, M = [int(x) for x in input().split()]

    MOD = 10 ** 9 + 7

    if abs(M - N) >= 2:
        print(0)
        return

    d = math.factorial(N)
    m = math.factorial(M)
    if N == M:
        d *= 2
    print(d * m % MOD)



    
    

if __name__ == '__main__':
    main()

Solution (Python):
import math

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

Solution (Python):
MOD = 10**9+7
import sys
sys.setrecursionlimit(100000)


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


n,m = map(int,input().split())
if abs(n-m) > 1:
    print(0)
elif abs(n-m) == 1:
    print(kaijo(m) * kaijo(n) % MOD)
else:
    print(pow(kaijo(m), 2, MOD)*2%MOD)

Solution (Python):
from math import factorial
n,m=map(int,input().split())
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 perm(n):
    a=1
    for i in range(1,n+1):
        a*=i*i
        a%=10**9+7
    return a

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

Solution (Python):
MOD=10**9+7
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self//value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

class Combination:
    def __init__(self,max_n):
        self.max_n=0
        self.fact=[Fp(1)]
        self.ifact=[Fp(1)]
        self.make_fact_list(max_n)
    def C(self,n,k):return self.fact[n]*self.ifact[k]*self.ifact[n-k] if 0<=k<=n else 0
    def H(self,n,k):return self.C(n+k-1,k) if n or k else 1
    def P(self,n,k):return self.fact[n]*self.ifact[n-k] if 0<=k<=n else 0
    def make_fact_list(self,max_n):
        if max_n<=self.max_n: return
        self.fact+=[Fp(0)]*(max_n-self.max_n)
        self.ifact+=[Fp(0)]*(max_n-self.max_n)
        for i in range(self.max_n+1,max_n+1):
            self.fact[i]=self.fact[i-1]*i
            self.ifact[i]=self.ifact[i-1]//i
        self.max_n=max_n

N, M = sorted(map(int, input().split()))
p = Combination(M)
ans = p.fact[N]*p.fact[M]
if N == M:
    ans *= 2
if abs(N-M) > 1:
    ans = 0
print(ans)

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

X = 10 ** 9 + 7
ans = 0

if N - M < 2:
    if N == M:
        ans = 2
    elif abs(N - M) == 1:
        ans = 1

    for i in range(N):
        ans *= N - i
        ans %= X
    for i in range(M):
        ans *= M - i
        ans %= X

print(ans)

Solution (Python):

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

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

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

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

MOD = 10**9 + 7
ans = 0
if abs(N-M) < 2:
    A, B = max(N, M), min(N, M)
    res = 1
    if A == B:
        for i in range(1, A+1):
            res = (i**2 % MOD) * res % MOD
        ans = res * 2 % MOD
    else:
        for a in range(1, A+1):
            res = res * a % MOD
        for b in range(1, B+1):
            res = res * b % MOD
        ans = res % MOD

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

fac = [-1] * (10**7+1)
finv = [-1] * (10**7+1)
inv = [-1] * (10**7+1)

fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

def initNCMMod(limit):
    for i in range(2, limit):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod // i) % mod
        finv[i] = finv[i-1] * inv[i] % mod

def NCMMod(n, k):
    if n < k:
        return 0
    if (n < 0 or k < 0):
        return 0
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod
initNCMMod(100001)

def main():
    N, M = LI()
    ans = 0
    if abs(N-M) == 1:
        ans = fac[N] * fac[M] % mod
    elif abs(N-M) == 0:
        ans = 2 * fac[N] * fac[N] % mod
    else:
        ans = 0
    print(ans)
main()

Solution (Python):
import math
N, M = map(int, input().split())
mod = 10**9 + 7
answer = 0
if abs(N - M) == 0:
    answer = 2 * math.factorial(N)**2
elif abs(N - M) == 1:
    answer = math.factorial(max(N, M)) * math.factorial(min(N, M))

print(answer % mod)

Solution (Python):

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

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

if not (n == m or n == m-1 or n == m+1):
    print(0)
    exit(0)


if n == m:
    print(math.factorial(n) % MOD * math.factorial(n) % MOD * 2 % MOD)
    exit(0)
print(math.factorial(n) % MOD*math.factorial(m) % MOD)

Solution (Python):

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

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

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

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

fac = [0]*MAX   
inv = [0]*MAX    
finv = [0]*MAX   

def COMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[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
        
    
def COM(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return (fac[n] * (finv[k]*finv[n-k]))%MOD


COMinit()    
n,m = map(int,input().split())
if abs(n-m) > 1:
  print(0)
elif abs(n-m) == 1:
  print(fac[n]*fac[m] %MOD)
else:
  print(2*fac[n]*fac[m] %MOD)

Solution (Python):
import sys
input = sys.stdin.readline
from math import factorial
mod=10**9+7
n,m=map(int,input().split())
if n>m:
    n,m=m,n
if n+1==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(n*m%mod)
elif n==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(2*n*m%mod)
else:
    print(0)

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

from math import factorial

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

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

fact = [-1] * (max(n, m) + 1)
for i in range(max(n, m) + 1):
  if i < 3:
    fact[i] = i
  else:
    fact[i] = i * fact[i - 1] % mod

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

Solution (Python):
import math
arr = input().split()
n = int(arr[0])
m = int(arr[1])

if n == m:
    print((math.factorial(n) * math.factorial(m) * 2) % (10**9 + 7))
elif n == m + 1 or m == n + 1:
    print((math.factorial(n) * math.factorial(m)) % (10**9 + 7))
else:
    print(0)

Solution (Python):
x,y=map(int,input().split())
import math
xx=math.factorial(x)
yy=math.factorial(y)
if x==y:
    print(xx*yy*2%((10**9)+7))
elif abs(x-y)==1:
    print(xx*yy%((10**9)+7))
else:
    print(0)

Solution (Python):

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

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

factorial = [1] * (max(N,M)+1)
for i in range(1,max(N,M)+1):
    factorial[i] = (factorial[i-1]*i) % MOD

if abs(N - M) == 0:
    ans = ((factorial[N]**2) * 2) % MOD
else:
    ans = (factorial[N] * factorial[M]) % MOD
print(ans)

Solution (Python):
p = 10 ** 9 + 7
N = (10 ** 5)*2 +10  
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)

n,m = map(int,input().split())
if abs(n-m) > 1:
    print(0)
elif n==m:
    m1 = fact[n]
    print(((m1**2)*2)%p)
else:
    m1 = fact[n]
    m2 = fact[m]
    print((m1*m2)%p)

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

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

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

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

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

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

fn=1
for n in range(2,min(N,M)+1):
    fn=fn*n%MOD
fx=fn

ans=1
if N==M:
    ans*=2
else:
    fx=fx*max(N,M)%MOD
print(ans*fn*fx%MOD)

Solution (Python):
def fact(a):
    b = 1
    for i in range(1,a+1):
        b = (b*i)%inf
    return b
inf = 10**9+7
N, M = map(int,input().split())
if abs(N-M) == 1:
    ans = fact(N)*fact(M)%inf
elif abs(N-M) == 0:
    ans = fact(N)*fact(M)*2%inf
else: ans = 0
print(ans)

Solution (Python):
from math import factorial
n,m = map(int,input().split())

dogs = factorial(n)
apes = factorial(m)

if n==m:
    ans = 2*dogs*apes
elif abs(n-m) == 1:
    ans = dogs*apes
else:
    ans = 0

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

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):
def factorial(n):
  fact = 1
  for i in range(2, n+1):
    fact = 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)

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


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

main()

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

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

Solution (Python):
N, M=map(int, input().split())
mod=10**9+7
def kaijou(x, mod):
  out=1
  for i in range(2, x+1):
    out*=i
    out%=mod
  return out

if N==M:
  print((((kaijou(N, mod)**2)%mod)*2)%mod)
  exit()
if abs(N-M)>1:
  print(0)
  exit()
  
print(((kaijou(N, mod)*kaijou(M, mod))%mod))

Solution (Python):
import math

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

if abs(N-M) >= 2:
    print(0)
    exit()
    
N_fac = math.factorial(N) % mod
M_fac = math.factorial(M) % mod

if N == M:
    print(N_fac * M_fac * 2 % mod)
else:
    print(N_fac * M_fac % mod)

Solution (Python):
import sys
n,m=map(int,input().split())
num=n
mum=m
if 2<=abs(n-m):
  print(0)
  sys.exit()
if abs(n-m)==1:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  while m>1:
    mum*=m-1
    m-=1
    mum=mum%((10**9)+7)
  print((num*mum)%((10**9)+7))
  sys.exit()
if abs(n-m)==0:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  print((2*(num**2))%((10**9)+7))
  sys.exit()

Solution (Python):
import math

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

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

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

if N==M:
  answer=2*math.factorial(N)**2%mod
elif abs(N-M)==1:
  answer=math.factorial(N)*math.factorial(M)%mod
print(answer)

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

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

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

ans = 1

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

Solution (Python):
import itertools

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

Mod = 1000000007
Sum = 0

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

    n = 1
    m = 1

    for i in range(N):
        n = (n * (i + 1)) % Mod
    for j in range(M):
        m = (m * (j + 1)) % Mod

    Sum = (n * m) % Mod

    if N == M:
        Sum = (Sum * 2) % Mod

    print(Sum)

Solution (Python):
import sys
import math
input = sys.stdin.readline
a = [int(x) for x in input().split()]
N = min(a)
M = max(a)
if N + 1 < M:
    print(0)
    sys.exit()
if N != M:
    print(math.factorial(N) * math.factorial(M) % (7 + 10 ** 9))
else:
    print(2 * math.factorial(N) * math.factorial(M) % (7 + 10 ** 9))

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

Solution (Python):
import math
a, b = map(int, input().split())
if abs(a - b) >= 2:
    print(0)
else:
    x = math.factorial(a)
    y = math.factorial(b)
    ans = x * y
    if a == b:
        print(ans * 2 % (10 ** 9 + 7))
    else:
        print(ans % (10 ** 9 + 7))

Solution (Python):
import math

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

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

Solution (Python):
def lam(x,mo):
	ans=1
	for i in range(1,x+1):
		ans*=i
		ans%=mo
	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,mod)*lam(m,mod)%mod)
else:
	print(2*(lam(n,mod)**2)%mod)

Solution (Python):
import sys

MOD = 10 ** 9 + 7

def make_factorial_mod(n, mod):
    fac = [None] * (n + 1)
    fac[0] = 1
    for i in range(n):
        fac[i+1] = fac[i] * (i + 1) % mod
    return fac

fac = make_factorial_mod(10**5, MOD)

n, m = map(int, sys.stdin.readline().split())
if n < m:
    n, m = m, n

def main():

    if n == m:
        return fac[n] * fac[m] * 2 % MOD
    elif n == m + 1:
        return fac[n] * fac[m] % MOD
    else:
        return 0

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
from math import factorial

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

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

n_frac = factorial(N) 
m_frac = n_frac
if N-M == 0:
    print((n_frac*m_frac)*2%mod)
else:
    if N > M:
        m_frac //= N
    else:
        m_frac *=  M
    print(n_frac*m_frac%mod)

Solution (Python):
N, M = map(int, input().split())
S = 0
mod = int(1e+9 + 7)
if abs(M - N) == 1:
  S = 1
  em = max(M, N)
  for i in range(1, em):
    S *= (i * i) % mod
    S %= mod
  S = (S * em) % mod
elif M == N:
  S = 1
  for i in range(1, M+1):
    S *= (i * i) % mod
    S %= mod
  S = (S * 2) % mod
print(S)

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

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

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

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

ans = 1
if n == m:
    for i in range(1,n+1):
        ans *= i
        ans %= 10**9 + 7
    for i in range(1,n+1):
        ans *= i
        ans %= 10**9 + 7
    ans *= 2
    ans %= 10**9 + 7
elif abs(n-m) == 1:
    for i in range(1,n+1):
        ans *= i
        ans %= 10**9 + 7
    for i in range(1,m+1):
        ans *= i
        ans %= 10**9 + 7
else:
    print(0)
    import sys
    sys.exit()

print(ans)

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

mod = 10 ** 9 + 7
ans = 1
if abs(N - M) >= 2:
    print(0)

else:
    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):
class ModWorld:
    def __init__(self, mod):
        self.mod = mod

    def prepare_factorial(self, N):
        self.f = [1] 
        p = 1
        for i in range(1,N+1):
            p = (p*i) % self.mod
            self.f.append(p)

    def modpow(self, x, k):
        x %= self.mod
        k %= self.mod-1 
        retval = 1
        while k:
            if k&1: retval *= x
            k>>=1
            x = x*x % self.mod
        return retval % self.mod

    def modcomb(self, n, k):
        return self.f[n] * self.modpow(self.f[n-k], -1) * self.modpow(self.f[k], -1) % self.mod

mw = ModWorld(mod=10**9+7)
mw.prepare_factorial(N=10**5)

N, M = list(map(int, input().split()))
d = 2 if N==M else 1 if abs(N-M)==1 else 0
print(d * mw.f[N] * mw.f[M] % mw.mod)

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

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

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, string
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
mod = 10**9+7
ans = 0 ;count = 0 ;pro = 1

n,m=map(int,input().split())
def factorial(n):
    result=1
    for ni in range(n,0,-1):
        result=result*ni%mod
    return result
    
if abs(n-m)>=2:
    print(0)
    exit()
if abs(n-m)==1:
    print(factorial(n)*factorial(m)%mod)
else:
    print(factorial(n)*factorial(m)*2%mod)

Solution (Python):
def c_reconciled(MOD=10**9 + 7):
    from math import factorial
    N, M = [int(i) for i in input().split()]

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

print(c_reconciled())

Solution (Python):
import math

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

if abs(n-m) > 1:
    ans = 0
elif abs(n-m) == 1:
    n_facto = math.factorial(n) % (10**9 + 7)
    m_facto = math.factorial(m) % (10**9 + 7)
    ans = (n_facto * m_facto) % (10**9 + 7)
else:
    n_facto = math.factorial(n) % (10**9 + 7)
    m_facto = math.factorial(m) % (10**9 + 7)
    ans = (n_facto * m_facto * 2) % (10**9 + 7)

print(ans)

Solution (Python):
mod = 10**9+7
def kai(n):
  i = n
  r = 1
  while i > 1:
    r = (r*i) % mod
    i -= 1
  return r
m,n = map(int,input().split())
m,n = max(m,n),min(m,n)
if m > n+1:
  print(0)
elif m == n+1:
  print((kai(n)**2*m)%mod)
else:
  print((2*kai(n)**2)%mod)

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

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

if n==m:
      print((math.factorial(n)*math.factorial(m)*2)%mod)
      exit()

print((math.factorial(n)*math.factorial(m))%mod)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md = 10 ** 9 + 7
    n, m = MI()
    fac = [1, 1]
    for i in range(2, n + 5):
        fac.append(fac[-1] * i % md)
    if abs(n - m) > 1:
        print(0)
        exit()
    if n < m: n, m = m, n
    if n == m:
        ans = 2 * fac[n] ** 2 % md
    else:
        ans = fac[n] * fac[m] % md
    print(ans)

main()

Solution (Python):
import sys
import math

sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

N, M = MAP()

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

dog = permutations_count(N, N) % (10**9 + 7)
monkey = permutations_count(M, M) % (10**9 + 7)

ans = dog*monkey % (10**9 + 7) if N!=M else dog*monkey*2 % (10**9 + 7)

print(ans)

Solution (Python):
import sys
def f(a):
    b=1
    for i in range(a):
        b*=i+1
        b%=1000000007
    return b
a=sorted(list(map(int,input().split())))
if a[1]-a[0]>=2:
    print(0)
    sys.exit()
c=2
if a[1]>a[0]:
    c=1
print((f(a[1])*f(a[0])*c)%1000000007)

Solution (Python):
import sys
def f(a):
    b=1
    for i in range(a):
        b*=i+1
        b%=1000000007
    return b
a=sorted(list(map(int,input().split())))
if a[1]-a[0]>=2:
    print(0)
    sys.exit()
c=2
if a[1]>a[0]:
    c=1
print((f(a[1])*f(a[0])*c)%1000000007)

Solution (Python):
N, M = map(int, input().split())
p = 10**9 + 7
if abs(N-M) > 1:
  print(0)
  exit()

fct = [1] * (max(N, M) + 1)
for i in range(2, max(N, M)+1):
  fct[i] = fct[i-1] * i % p

print(fct[N] * fct[M] * ((N == M) + 1) % p)

Solution (Python):
import sys
MOD = 1e9 + 7
N, M = map(int, input().split())
ans = 1
if abs(N - M) > 1:
    print(0)
    sys.exit()
for n in range(2, N + 1):
    ans *= n
    ans %= MOD
for m in range(2, M + 1):
    ans *= m
    ans %= MOD
if N == M:
    ans *= 2
    ans %= MOD
print(int(ans))

Solution (Python):
from math import factorial

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

M = 10 ** 9 + 7

if abs(n - m) > 1:
    print(0)
else:
    ans = factorial(m) * factorial(n) % M
    if (m + n) % 2 == 0:
        ans = ans * 2 % M
    print(ans)

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

Solution (Python):
import math

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

elif abs(n - m) == 1:
    print(a * b % 1000000007)
else:
    print(0)

Solution (Python):
import math

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

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

n_cnt = 1
m_cnt = 1
if abs(n-m) == 1:
    for i in range(1, n+1):
        n_cnt *= i
        n_cnt %= MOD
    for i in range(1, m+1):
        m_cnt *= i
        m_cnt %= MOD
    print(n_cnt*m_cnt%MOD)
    exit()

n_cnt = 1
m_cnt = 1
if n == m:
    for i in range(1, n+1):
        n_cnt *= i
        n_cnt %= MOD
    for i in range(1, m+1):
        m_cnt *= i
        m_cnt %= MOD
    print(2*n_cnt*m_cnt%MOD)

Solution (Python):
N , M = map(int,input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
    print("0")
    exit()
large = max(N , M)
small = min(N , M)
N_fuct = 1
M_fuct = 1
for i in range(2,large + 1):
    N_fuct *= i
    N_fuct %= MOD
for i in range (2,small + 1):
    M_fuct *= i
    M_fuct %= MOD
ans = N_fuct * M_fuct
if large == small:
    ans *= 2
    print(ans % MOD)
else:
    print(ans % MOD)

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

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


if n-1 > m:
  print(0)
  exit()
  
if n == m:
  ans = (math.factorial(n)%MOD) * (math.factorial(m)%MOD)*2
  print(ans%MOD)
  
elif n-1 == m:
  ans = (math.factorial(n)%MOD) * (math.factorial(m)%MOD)
  print(ans%MOD)

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

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


if n-1 > m:
  print(0)
  exit()
  
if n == m:
  ans = math.factorial(n)* math.factorial(m)*2
  print(ans%MOD)
  
elif n-1 == m:
  ans = math.factorial(n)* math.factorial(m)
  print(ans%MOD)

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

Solution (Python):
import sys
n, m = [int(i) for i in sys.stdin.readline().split()]
MOD = 10**9+7

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

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

Solution (Python):
def main():
    nn,mm=map(int,input().split())
    n=max(nn,mm)
    m=min(nn,mm)
    temp=10**9 +7
    if n-m>=2:
        print(0)
        return
    fact=[0,1]
    for i in range(2,n+1):
        fact.append(fact[i-1]*i%temp)
    ans=fact[n]*fact[m]%temp
    if n-m==0:
        ans=ans*2%temp
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):

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

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

Solution (Python):
n,m = map(int,input().split())
mod = 10**9+7
def fact(x):
    a = 1
    for i in range(1,x+1):
        a = (a*i)%mod
    return a
if n >m+1 or m>n+1:
    print(0)
  

elif n == m+1:
    p = (fact(m)**2)%mod
    print(p*(m+1)%mod)

elif m == n+1:
    p = (fact(n)**2)%mod
    print(p*(n+1)%mod)
elif m == n:
    p = fact(n)**2%mod
    print(p*2%mod)