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 math

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

b = 10**9 + 7

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

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

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

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

Solution (Python):
from sys import exit

divisor = 1000000007

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

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

k = min(N, M)
t = 1
for i in range(2, k + 1):
    t = t * i % divisor
t = t * t % divisor

if N == M:
    print(2 * t % divisor)
else:
    print(t * (k + 1) % divisor)

Solution (Python):
def input():
    import sys
    return sys.stdin.readline().rstrip()


def factorial(n, DIVISOR):
    import functools
    return functools.reduce(lambda x, y: (x % DIVISOR) * y, range(1, n + 1)) % DIVISOR


def main():
    import math
    import collections
    import itertools
    import functools

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


if __name__ == '__main__':
    main()

Solution (Python):


import math

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

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

Solution (Python):
mod=1000000007
def kai(n):
    a=1
    for i in range(1,n+1):
        a*=i
        a%=mod
    return a%mod
n,m=map(int,input().split())
if abs(n-m)==1:
    print(kai(n)*kai(m)%mod)
elif abs(n-m)==0:
    print(2*kai(n)*kai(m)%mod)
else:
    print(0)

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    t = 1
    for i in range(2, n + 1):
        t = (t * i) % (10 ** 9 + 7)
    for i in range(2, m + 1):
        t = (t * i) % (10 ** 9 + 7)
    if abs(n - m) == 1:
        print(t)
    elif abs(n - m) == 0:
        t = (t * 2) % (10 ** 9 + 7)
        print(t)
    else:
        print(0)


if __name__ == '__main__':
    slove()

Solution (Python):
from math import factorial

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

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

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

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

if n - m > 1:
    print(0)
    exit()
elif n - m == 1:
    rev = 1
else:
    rev = 2

mod = 10**9 + 7
c_n = 1
c_m = 1
t = n
while t > 1:
    c_n *= t
    c_n %=mod

    if t > m:
        t -= 1
        continue

    c_m *= t
    c_m %= mod
    t -= 1 

ans = c_n * c_m * rev % mod
print(ans)

Solution (Python):

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

if abs(N - M) >= 2:
    print(0)
else:
    for i in range(N):
        ans_N = ans_N * ((M - i) * (N - i)) % mod
    for j in range(M):
        ans_M = ans_M * ((M - j) * (N - j)) % mod
    print((ans_N+ans_M)% mod)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def p(n):
    if n==1:
        return 1
    return n*p(n-1)%(10**9+7)
n,m=map(int,input().split())
if n==m:
    print(p(n)**2*2%(10**9+7))
elif abs(n-m)==1:
    print(p(n)*p(m)%(10**9+7))
else:
    print(0)

Solution (Python):
import sys
sys.setrecursionlimit(int(1e6))

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

base = int(1e9 + 7)
ans = 0
if abs(N -M) > 1:
  pass
else:
  fact = 1
  for i in range(1, min(N+1, M+1)):
    fact = (fact * i) % (base)
  
  if N == M:
    ans = 2 * fact * fact
  else:
    ans = fact * fact * max(N, M)
    
ans = ans % base
print(ans)

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

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

tmpN = math.factorial(N)
tmpM = math.factorial(M)
ans = tmpN * tmpM
if N == M:
    ans *= 2
print(ans % INF)

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

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

tmpN = math.factorial(N)
tmpM = math.factorial(M)
ans = tmpN * tmpM
if N == M:
    ans *= 2
print(ans % INF)

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

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

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

Solution (Python):
def prod(n):
    x = 1
    for i in range(1, n+1):
        x *= i
        x %= (10 ** 9 + 7)
    return x


n, m = map(int, input().split())
n, m = min(n, m), max(n, m)
minusNM = abs(n-m)
ans = 0
if minusNM == 0:
    ans = 2 * prod(n) ** 2
elif minusNM == 1:
    ans = prod(n) ** 2 * m
print(ans % (10 ** 9 + 7))

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

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

Solution (Python):
import math
n, m = map(int,input().split())
mod = (10**9)+7
mul = 0 if abs(n-m)>1 else 2 if n==m else 1
a = min(n,m)
A = math.factorial(a)
B = A
if mul==1:
  B*=max(n,m)
print(A*B*mul%mod)

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

Solution (Python):
N,M=map(int,input().split())
MOD=10**9+7
arr=[1]
for i in range(1,N+M+1):
    arr.append((arr[-1]*i)%MOD)
s=arr[N]*arr[M]
if N==M:
    print(2*s%MOD)
elif abs(N-M)==1:
    print(s%MOD)
else:
    print(0)

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

Solution (Python):
n, m = map(int, input().split())
mod = 10**9 + 7
if abs(n - m) <= 1:
    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 abs(n-m) == 1:
        print(ans)
    else:
        print(ans * 2 % mod)
else:
    print(0)

Solution (Python):
from math import factorial as F
N, M = map(int,input().split())
MOD = 10**9+7
if N == M:
  print(F(N) * F(M) % MOD * 2 % MOD)
elif N + 1 == M or M + 1 == N:
  print(F(N) * F(M) % MOD)
else:
  print(0)

Solution (Python):
def main():
    mod = 10**9+7
    n,m=map(int,input().split(' '))
    ans = 1
    if abs(n-m)>1:
        ans = 0
        print(ans)
        exit()
    if n==m:
        ans *= 2
    for i in range(1,n+1):
        ans *= i
        ans %=mod
    for j in range(1,m+1):
        ans *= j
        ans %= mod
    print(ans)
if __name__ == '__main__':
    main()

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


def for_factorial(n):
    mod = 10**9+7
    val = 1
    for i in range(2, n + 1):
        val = (val*i) % mod
    return val


f = for_factorial(N)*for_factorial(M) % mod
if N == M:
    ans = f*2 % mod
elif abs(N - M) == 1:
    ans = f % mod
else:
    ans = 0

print(ans)

Solution (Python):
import math

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

if abs(n-m) == 1:
    print((math.factorial(n)*math.factorial(m)) % (10**9+7))
elif n == m:
    print((math.factorial(n)*math.factorial(m)*2) % (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()

li = sorted([n,m])

num = 1
for i in range(1,li[0]+1):
    num = (num * i) % mod
    
if li[1] - li[0] == 1:
    print((((num * li[1]) % mod) * num) % mod)
else:
    print((((num * num) % mod) * 2) % mod)

Solution (Python):
from scipy.misc import comb
import math
mod = 10**9 + 7

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

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

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


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


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

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

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

Solution (Python):
import math

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

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

if abs(N-M)>=2:
    print(0)
else:
    mod = 10**9 + 7
    if abs(N-M)==1:
        w = 1
    else:
        w = 2
    nf = 1
    mf = 1
    
    while N > 1:
        nf *= N
        nf %= mod
        N -= 1

    while M > 1:
        mf *= M
        mf %= mod
        M -= 1
    
    ans = nf * mf % mod
    ans = ans * w % mod

    print(ans)

Solution (Python):
import math
N, M = tuple(map(int, input().split()))
if N == M:
    print(2 * math.factorial(N) * math.factorial(M) % (10**9 + 7))
    exit()

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

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


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

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

if N==M+1 or N==M-1:
    ans=(math.factorial(N)*math.factorial(M))%mod
elif N==M:
    ans=((math.factorial(N)**2)*2)%mod


print(ans)

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

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

sn,sm = 1,1
for i in range(1,n+1):
    sn = sn*i%mod
for i in range(1,m+1):
    sm = sm*i%mod

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

Solution (Python):


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

mod = 10 ** 9 + 7


def factorial(x):
    ret = 1
    for i in range(1, x + 1):
        ret *= i
        ret %= mod

    return ret


def solve(m, n):
    if m > n:
        m, n = n, m
    if n - m > 1:
        return 0
    fct = factorial(m)
    if n - m == 1:
        return fct ** 2 * n % mod
    else:  
        return fct ** 2 * 2 % mod


ans = solve(m, n)
print(ans)

Solution (Python):


from math import factorial

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


def solve(m, n):
    if m > n:
        m, n = n, m
    if n - m > 1:
        return 0
    fct = factorial(m)
    if n - m == 1:
        return fct ** 2 * n
    else:  
        return fct ** 2 * 2


mod = 10 ** 9 + 7
ans = solve(m, n) % mod
print(ans)

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


a=math.factorial(N)*math.factorial(M)
if abs(N-M)>1:
    print(0)
elif N>M or M>N:
    print(int(a%(1000000000+7)))
elif M==N:
    print(int(a*2%(1000000000+7)))

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


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

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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

n,m = inpl()
if n == m:
    print((nnn(n) **2 *2)%mod)
elif abs(n-m) == 1:
    print((nnn(n) * nnn(m))%mod)
else:
    print(0)

Solution (Python):
from sys import stdin
import math
N,M = [int(x) for x in stdin.readline().rstrip().split()]
mod = 10**9+7
nm = abs(N-M)
if nm == 0:
    ans = math.factorial(N)**2*2
    ans %= mod
    print(ans)
elif nm == 1:
    ans = math.factorial(N)*math.factorial(M)
    ans %= mod
    print(ans)
else:
    print(0)

Solution (Python):

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

Solution (Python):
import math

MOD = 10**9 + 7

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

Solution (Python):
from math import factorial as f
N,M = map(int,input().split())
print(max(0,2-abs(N-M))*f(N)*f(M)%(10**9+7))

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from math import factorial
    n,m=map(int,input().split())
    if abs(n-m)>1:
        print(0)
    elif abs(n-m)==1:
        print(factorial(n)%MOD*factorial(m)%MOD)
    else:
        print(2*factorial(n)%MOD*factorial(m)%MOD)
resolve()

Solution (Python):
import math


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


if __name__ == "__main__":
    resolve()

Solution (Python):
import math as m

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

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

  Pn=1
  for i in range(1,N+1):
    Pn*=i
    Pn%=10**9+7

    
  print((Pm*Pn*1)%(10**9+7) if M!=N else (Pm*Pn*2)%(10**9+7))

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

big = max(n,m)
small = min(n,m)

mod = 10**9+7
if big > small+1:
    print(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
    print(ans%mod)

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

Solution (Python):
from math import factorial
n,m=map(int,input().split())
if n>m+1 or n+1<m:
  print(0)
  exit()

if n == m:
  print(2*((factorial(n)%1000000007)*(factorial(m)%1000000007))%1000000007)
if n != m:
  print(((factorial(n)%1000000007)*(factorial(m)%1000000007))%1000000007)

Solution (Python):
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
mod = 10**9 + 7
n, m = map(int, input().split())
diff = max(n, m) - min(n, m)

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

inup = math.factorial(n)
sarup = math.factorial(m)
if diff == 0:
    val = inup * sarup * 2
    print(val%mod)
elif diff == 1:
    val = inup * sarup
    print(val%mod)

Solution (Python):
import math

N,M = list(map(int, input().split()))
if abs(N-M) >= 2:
	print(0)
else:
	if N==M:
		print(math.factorial(N)**2*2%1000000007)
	else:
		print(math.factorial(N)*math.factorial(M)%1000000007)

Solution (Python):

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


import math


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

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

Solution (Python):
N, M = [int(x) for x in input().split()]
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
  ret = 0
else:
  ret = 1
  for i in range(1, min(N, M) + 1):
    ret = (ret * i) % MOD
  else:
    ret = (ret * ret) % MOD
  for i in range(min(N, M) + 1, max(N, M) + 1):
    ret = (ret * i) % MOD
  if N == M:
    ret = (ret * 2) % MOD
print(ret)

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

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

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

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

if abs(N-M) == 0:
    tmp = p(N)
    ans = tmp * tmp * 2 % MOD
elif abs(N-M) == 1:
    tmp = p(min(N,M))
    ans = tmp * tmp *max(N,M) % MOD
else:
    ans = 0

print(ans)

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

Solution (Python):
N,M=map(int,input().split());a=abs(N-M);p=[0,1,2][(a<1)+(a<2)];m=10**9+7
for i in range(1,N+1):p=p*i%m
for i in range(1,M+1):p=p*i%m
print(p)

Solution (Python):
from math import factorial

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

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

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

Solution (Python):

import sys
from math import factorial

MOD = 1000000007  

def solve(N: int, M: int):
    if(N == M):
        print((2*factorial(N)*factorial(M))%MOD)
    elif(N == M-1 or M == N-1):
        print((factorial(N)*factorial(M))%MOD)
    else:
        print(0)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    solve(N, M)

if __name__ == '__main__':
    main()

Solution (Python):
import math
mod=1000000007
N,M=map(int,input().split())
if abs(N-M)>1:
  print(0)
else:
  n=math.factorial(N)
  m=math.factorial(M)
  if n==m:
    print(2*n*m%mod)
  else:
    print(n*m%mod)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

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

cond = N == M or N+1 == M or N == M+1
if cond == False:
	print(0)
	sys.exit()

def factorial(a):
	ans = 1
	for i in range(1,a+1):
		ans *= i
		ans %= 1000000007
	return ans

if N == M:
	h = factorial(N)
	print((((h*h)%1000000007)*2)%1000000007)
else:
	h_1 = factorial(N)
	h_2 = factorial(M)
	print((h_1*h_2) % 1000000007)

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

if abs(n-m)>=2:
    ans=0
else:
    nn=math.factorial(n)
    mm=math.factorial(m)
    ans = nn*mm
print(0 if ans==0 else ans%(10**9+7) if nn !=mm else (ans*2)%(10**9+7))

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

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

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

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

M = [1]
mul = 1
for i in range(1, 10**5+5):
    mul = (mul * i) % mod
    M.append(mul)

if n-m < -1 or n-m > 1:
    print(0)
elif n-m != 0:
    print((M[n]*M[m]) % mod)
else:
    print((((M[n]*M[m]) % mod) * 2 ) % mod)

Solution (Python):
from math import factorial

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

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

ans = factorial(N)
ans %= (10 ** 9 + 7)
ans *= factorial(M)
ans %= (10 ** 9 + 7)
if N == M:
    ans *= 2
    ans %= (10 ** 9 + 7)
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))
elif abs(N - M) == 0:
    print(2 * math.factorial(N) * math.factorial(M) % (10 ** 9 + 7))

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

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

import math

mod = 10**9 + 7

if N == M:
    ans = ((math.factorial(N) * math.factorial(M)) * 2) % mod
else:
    ans = (math.factorial(N) * math.factorial(M)) % mod

print(ans)

Solution (Python):
import math

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

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

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

print(ans)

Solution (Python):
import math
n, m = [int(el) for el in input().split(' ')]
prime = 10**9 + 7

def f(x):
    return(math.factorial(x) % prime)

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

Solution (Python):
import sys
import copy
import  math
import bisect
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

mod = 10 ** 9 + 7
def calc(num):
    ans = 1
    for i in range(1, num+1):
        ans *= i
        ans %= mod
    return ans

n, m = na()
if abs(n - m) >= 2:
    print(0)
else:
    inu = calc(n)
    saru = calc(m)
    if n == m:
        print(inu * saru * 2 % mod)
    else:
        print(inu * saru % mod)

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

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


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

Solution (Python):
import math

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

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

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

Solution (Python):
import math

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

dog = math.factorial(n)%(10**9+7)
monkey = math.factorial(m)%(10**9+7)

if abs(n-m)>1:
  print(0)
elif n==m:
  print(2*dog*monkey%(10**9+7))
else:
  print(dog*monkey%(10**9+7))

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

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

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

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

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

import math

if abs(N-M)>=2:
  print(0)
else:
  if (N+M)%2==0:
    print((2*(math.factorial(N)%1000000007)*(math.factorial(M)%1000000007))%1000000007)
  else:
    print(((math.factorial(N)%1000000007)*(math.factorial(M)%1000000007))%1000000007)

Solution (Python):
A,B=sorted(map(int,input().split()))
mod=10**9+7

if B-A>1:
    print(0)

elif B-A==1:
    ANS=1
    
    for i in range(1,A+1):
        ANS=ANS*i%mod

    for i in range(1,B+1):
        ANS=ANS*i%mod

    print(ANS)

else:
    ANS=1
    
    for i in range(1,A+1):
        ANS=ANS*i%mod

    for i in range(1,B+1):
        ANS=ANS*i%mod

    print(ANS*2%mod)

Solution (Python):
import math
N,M = map(int,input().split())
if N == M:
    print((2*math.factorial(N)**2)%1000000007)
elif N == M-1 or N == M+1:
    print((math.factorial(M)*math.factorial(N))%1000000007)
else:
    print(0)

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

mod = 10**9+7

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

tmp = 1

for i in range(n):
    tmp = tmp * (i+1) % mod
    
for i in range(m):
    tmp = tmp * (i+1) % mod
    
if n == m:
    print( tmp*2 % mod)
else:
    print(tmp)

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

Solution (Python):


import math




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


if abs(N-M) <= 1:
    dog_p = math.factorial(N)
    monky_p = math.factorial(M)
    work = ((dog_p % mod) * (monky_p % mod)) % mod

    if N != M:
        ans = work
    else:
        ans = ((work % mod) * (2 % mod)) % mod
else:
    
    ans = 0

print(ans)

Solution (Python):
x,y=map(int,input().split())

def arrange(x,y):
    if x>y:
        x,y=y,x
    if y-x>1:
        return 0

    ans=1
    mod=10**9+7
    for i in range(1, x+1):
        ans*=i
        ans%=mod
    for i in range(1, y+1):
        ans*=i
        ans%=mod
    if x==y:
        ans*=2
        ans%=mod
    return ans

print(arrange(x,y))

Solution (Python):
import math

MOD = 10 ** 9 + 7

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

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

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

Solution (Python):
from math import factorial


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

Solution (Python):
def main():
    N, M = map(int, input().split())
    div = pow(10, 9) + 7
    fN, fM = 1, 1
    for i in range(1, N+1): fN = (i*fN) % div
    for i in range(1, M+1): fM = (i*fM) % div

    if N == M:
        return 2*fN*fM % div
    elif N == M+1 or N+1 == M:
        return fN*fM % div
    else:
        return 0


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

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

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

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

MOD = 10**9 + 7
def fact(x):
    res = 1
    for i in range(2, x+1):
        res *= i
        res %= MOD
    return res

if abs(n - m) > 1:
    ans = 0
elif abs(n - m) == 1:
    ans = (fact(n) * fact(m)) % MOD
else:
    
    ans = (fact(n) * fact(m) * 2) % MOD

print(ans)

Solution (Python):
import math

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):
N, M = map(int, input().split())
if abs(N-M) >= 2:
  print(0)
  exit()
if abs(N-M) == 1:
  r = 1
else:
  r = 2
for i in range(1, N+1):
  r = r*i%1000000007
for i in range(1, M+1):
  r = r*i%1000000007
print(r)

Solution (Python):
import math
n,m=map(int,input().split())
print((2*math.factorial(n)**2 if n==m else math.factorial(max(m,n))*math.factorial(min(m,n)) if abs(m-n)==1 else 0)%1000000007)