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):
n,m=map(int,input().split())
if abs(n-m)>1:
	ans=0
elif n!=m:
	ans=1
	for i in range(n):
		ans*=i+1
		ans%=10**9+7
	for i in range(m):
		ans*=i+1
		ans%=10**9+7
else:
	ans=2
	for i in range(2):
		for j in range(n):
			ans*=j+1
			ans%=10**9+7
print(ans)

Solution (Python):

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()

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

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

res = 0
if n == m:
    res = factorial(n) * factorial(m) * 2 % mod
else:
    res = factorial(n) * factorial(m) % mod

print(res)

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

n_pattern = 1
m_pattern = 1

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

for m in range(1, M+1):
  m_pattern *= m
  m_pattern = m_pattern % (10 ** 9 + 7)
  
if N > M + 1 or N < M - 1:
  print(0)
elif N == M + 1 or M == N + 1:
  print(n_pattern * m_pattern % (10 ** 9 + 7))
elif N == M:
  print(n_pattern * m_pattern * 2 % (10 ** 9 + 7))

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))


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


if abs(N - M) >= 2:
    print(0)
    exit()
if abs(N - M) == 1:
    a = factorial(N) % MOD * factorial(M) % MOD
else:
    a = factorial(N) % MOD * factorial(M) % MOD * 2 % MOD

print(a)

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

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

x = 10**9+7

if abs(n-m) >= 2:
  print(0)
  exit()
  
import math
if n == m:
  print((2*math.factorial(n)*math.factorial(m))%x)
else:
  print((math.factorial(n)*math.factorial(m))%x)

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

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

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

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

fact = [1 for _ in range(M + 100)]
for i in range(1, M + 100):
    fact[i] = (fact[i - 1] * i) % MOD

if N == M:
    ans = fact[N] * fact[N] % MOD * 2 % MOD
else:
    ans = fact[N] * fact[M] % MOD

print(ans)

Solution (Python):
import math
N,M=map(int,input().split())
m=min(N,M)
M=max(N,M)
r=10**9+7
if M-m>1:print(0)
if M-m==1:print(math.factorial(M)**2//M%r)
if M==m:print(2*math.factorial(M)**2%r)

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

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

if N < M :
    N, M = M, N
    
if N - M == 0 :
    ret = 1
    for i in range(1, N + 1) :
        ret = ret * i * i % mod
    print(ret * 2 % mod)
    
elif N - M == 1 :
    ret = 1
    for i in range(1, M + 1) :
        ret = ret * i * i % mod
    print(ret * N % mod)
else :
    print(0)

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

Solution (Python):

import sys
INF = float("inf")

MOD = 1000000007  


def fact(a):
    ans = 1
    while a > 0:
        ans *= a
        ans %= MOD
        a -= 1
    return ans


def solve(N: int, M: int):
    if abs(N-M) > 1:
        print(0)
        return
    if N == M:
        print(2*pow(fact(N), 2) % MOD)
    else:
        print(max(N, M)*pow(fact(min(N, M)), 2) % MOD)

    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):
a = list(map(int,input().split()))
n,m = max(a),min(a)
mod = 10**9+7
if n - m == 0:
  ans = 2
elif n - m == 1:
  ans = 1
else:
  ans = 0
for i in range(1,n+1):
  ans *= i
  ans %= mod
for i in range(1,m+1):
  ans *= i
  ans %= mod
print(ans)

Solution (Python):
import math
import time

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

st = time.time()

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

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

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

print(ans)

ed = time.time() -st

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

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

Solution (Python):



def mod(n):
    return n%(10**9+7)

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

n,m=map(int,input().split())
diff=abs(n-m)
if diff>1:ans=0
elif diff==0:
    ans=mod(2*fac(n)*fac(m))
else:
    ans=mod(fac(n)*fac(m))
print(ans)

Solution (Python):


from math import factorial

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

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

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

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

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

elif abs(N - M) <= 1 and N != M:
    print(math.factorial(N)*math.factorial(M)%mod)

else:
    print(0)

Solution (Python):

import sys, math, fractions, itertools

MOD = 1000000007  

f = [1]

def solve(N: int, M: int):
    if abs(N - M) == 1:
        print((f[N]*f[M]) % MOD)
    elif N == M:
        print(2 * (f[N]*f[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))  

    for i in range(1, 200000):
        f.append(f[-1] * i % MOD)
    solve(N, M)

if __name__ == '__main__':
    main()

Solution (Python):

import math

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

print(ret)

Solution (Python):

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

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

    if N == M - 1:
        print((fact(N) * fact(M)) % 1000000007)
        return
    elif N == M:
        print((fact(N) * fact(M) * 2) % 1000000007)
    else:
        print(0)
    
main()

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

Solution (Python):


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

if __name__ == '__main__':
    N, M = map(int,(input().split()))
    main(N,M)

Solution (Python):
import math
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))

Solution (Python):
N, M = map(int, input().split())
mod = int(1E9+7)

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

print(ans)

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

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

Solution (Python):
import math

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

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

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

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

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

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

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

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

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

Solution (Python):
from math import *

n, m = map(int, input().split())
fac_n = factorial(n) % (10 ** 9 + 7)
fac_m = factorial(m) % (10 ** 9 + 7)

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

print(0 if abs(n - m) > 1 else ans if n != m else (ans * 2)  % (10 ** 9 + 7))

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

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

def modPow(x,n):
    ans = 1
    while n >= 1:
        if bin( n & 1)==bin(1):
            ans = (ans*x) % BIGMOD
        x = x * x % BIGMOD
        n = n>>1
    return ans % BIGMOD

def modFact(N):
    fact = []
    fact.append(1) 
    for i in range(1,N+1):
        a = fact[i-1]*i % BIGMOD
        fact.append(a)
    return fact

def modInvFact(fact):
    invFact = []
    for i in range(1,N+2):
        a = modPow(fact[i-1],BIGMOD-2)
        invFact.append(a)
    return invFact

def modPlus(a,b):
    return (a + b) % BIGMOD

def modMinus(a,b):
    if a-b >= 0:
        return (a - b) % BIGMOD
    else:
        return (a - b + BIGMOD)
    
def modMulti(a,b):
    return a * b % BIGMOD


def nCr(fact,invFact,n,r):
    return (fact[n] * invFact[r] * invFact[n-r] ) %BIGMOD

N,M = map(int,input().split())
fact = modFact(max(N,M))
factN = fact[N]
factM = fact[M]
ans = 0
if abs(N-M) == 0:
    ans = modMulti(factN,factM)*2 % BIGMOD
elif abs(N-M) == 1:
    ans = modMulti(factN,factM)
else:
    ans = 0
    
print(ans)

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

MOD = int(1e9) + 7

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

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

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

    MOD = int(1e9) + 7

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

if __name__ == "__main__":
    main()

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

MOD = int(1e9) + 7

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

Solution (Python):



import math
import sys
sys.setrecursionlimit(100000000)
n,m = map(int,input().split())
mod = 10**9+7

x = max(n,m)
y = min(n,m)
if n-1 > m or m-1 > n:
    print(0)
    quit()

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

a = math.factorial(x)
b = P(y,x-1)
if y-(x-1) == 0:
    c = 1
else:
    c = 2*math.factorial(y-(x-1))
print(a*b*c % mod)

Solution (Python):
import math
n,m=map(int,input().split())
INF=10**9+7
if abs(n-m)>=2:ans=0
elif abs(n-m)==1:ans=math.factorial(n)*math.factorial(m)%INF
else:ans=2*math.factorial(n)**2%INF
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(M - N) == 1:
    print(math.factorial(N) * math.factorial(M) % MOD)
else:
    print(0)

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


main()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(200000)
import math

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

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

    
ans = 0
fn = math.factorial(n)
fm = math.factorial(m)
if n==m:
    ans = fn*fm*2
elif abs(n-m) == 1:
    ans = fn*fm
else:
    ans = 0
print(ans%MOD)

Solution (Python):
import math


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


if max(N,M) - min(N,M) >= 2:

    print(0)

elif max(N,M) - min(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):
import math
n,m=map(int,input().split())
mod=10**9+7

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

ans=0
if n==m:
    ans=2*math.factorial(n)*math.factorial(n)
else:
    ans=math.factorial(n)*math.factorial(m)
print(ans%mod)

Solution (Python):
from math import factorial
def mul(a, b):
    mod = 1000000007
    return ((a % mod) * (b % mod)) % mod
N,M = map(int,input().split())
if abs(N-M) >= 2:
    print(0)
elif N == M:
    k = factorial(N) % (10**9+7)
    ans = mul(k,k)
    print(mul(2,ans))
else:
    k = factorial(M) % (10**9+7)
    l = factorial(N) % (10**9+7)
    ans = mul(k,l)
    print(ans)

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

Solution (Python):

def kaijo(num, mod):
    now_num = 1
    for i in range(1, num + 1):
        now_num *= i
        now_num %= mod
    return now_num

def main():
    dog, monky = map(int, input().split())
    mod = 10 ** 9 + 7

    if abs(dog - monky) > 1:
        print(0)
    else:
        dog_num = kaijo(dog, mod)
        monky_num = kaijo(monky, mod)

        if dog == monky:
            ans = dog_num * monky_num * 2
        else:
            ans = dog_num * monky_num
        print(ans % mod)




if __name__ == '__main__':
    main()

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

Solution (Python):
import math

i = list(map(int,input().split()))
N = i[0]
M = i[1]

MOD = 1000000000 + 7

diff = abs(max(M,N) - min(M,N))

if diff >= 2:
    print(0)
elif diff == 1:
    ans = (math.factorial(max(M,N)) % MOD) * (math.factorial(min(M,N)) % MOD)
    print(ans % MOD)
else:
    ans = (math.factorial(max(M,N)) % MOD) * (math.factorial(min(M,N)) % MOD) * 2
    print(ans % MOD)

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

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=(int(i) for i in input().split())


mod=10**9+7
ans=1

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

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


mod=10**9+7
ans=1

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

Solution (Python):
n, m = [int(_) for _ in input().split()]


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


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

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

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

Solution (Python):

def factorial_mod(x: int) -> int:
    MOD = 10 ** 9 + 7  
    ret = 1
    for i in range(1, x + 1):
        ret = (ret * i) % MOD
    return ret


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    n, m = min(n, m), max(n, m)
    dif = m - n
    if dif > 1:  
        print(0)
    else:
        if dif == 0:  
            ans = (factorial_mod(n) ** 2 * 2) % MOD  
        else:  
            ans = (factorial_mod(n) ** 2 * m) % MOD  
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math

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

l = abs(N - M)


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

if l == 1:

    if N < M:
        _n = math.factorial(N)
        _m = _n * (M)
        ans = _n * _m
        print(ans%(10**9+7))
        exit()

    else:
        _m = math.factorial(M)
        _n = _m * (N)
        ans = _n * _m
        print(ans%(10**9+7))
        exit()



_n = math.factorial(N)


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

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

def solve(k):
    ans = 1
    for i in range(1,k+1):
        ans = (ans*i)%(10**9+7)
    return ans


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

Solution (Python):
import math

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

if abs(N - M) > 1:
    print(0)
    exit()
else:
    x = math.factorial(min(N, M))
    if N == M:
        ans = 2 * x * x % (10 ** 9 + 7)
    else:
        ans = x * x * max(N, M) % (10 ** 9 + 7)
    print(ans)

Solution (Python):
import math
n,m = map(int,input().split())
if abs(n-m) != 0 and abs(n-m) != 1 :
    print(0)
else:
    x = math.factorial(min(m,n))

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

Solution (Python):
N,M=map(int,input().split())
MOD=10**9+7
if abs(N-M)>=2:
    print(0)
    quit()
if abs(N-M)==1:
    a=1
else:
    a=2
def factorials(M):
    INF=int(1e9+7)
    fac=[0]*(M+1)
    finv=[0]*(M+1)
    inv=[0]*(M+1)
    fac[0]=fac[1]=1
    finv[0]=finv[1]=1
    inv[1]=1
    for i in range(2,M+1):
        fac[i]=(fac[i-1]*i)%INF
        inv[i]=INF-(inv[INF%i]*(INF//i))%INF
        finv[i]=finv[i-1]*inv[i]%INF
    return fac,finv
fac,finv=factorials(max(N,M))
print((a*fac[N]*fac[M])%MOD)

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

dec = abs(N-M)

if dec > 1:
    ans = 0
else:
    idx = max(N,M)
    dp = [1 for _ in range(idx+1)]
    dp[0] = 1
    for i in range(idx):
        dp[i+1] = dp[i]*(i+1)
        dp[i+1] %= dev

    if dec == 1:
        ans = dp[idx]*dp[idx-1]
        ans %= dev
    else:
        ans = 2*dp[idx]*dp[idx]
        ans %= dev

print(int(ans))

Solution (Python):
N, M = [int(s) for s in input().split()]

def main():
    n = N
    m = M
    d = 1000000007
    if abs(n - m) > 1:
        print(0)
        return
    n, m = (m, n) if n > m else (n, m)
    ans = 1
    for i in range(1, n + 1):
        ans *= i ** 2
        ans = ans % d
    last_n = 2 if n == m else m
    ans = ans * last_n % d
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  

def solve(N: int, M: int):
    def fact(n):
        ret = 1
        for i in range(2, n + 1):
            ret *= i
            ret %= MOD
        return ret
    if abs(N - M) > 1:
        ret = 0
    else:
        ret = fact(N) * fact(M)
        if N == M:
            ret *= 2
    print(ret % MOD)
    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):
N,M=map(int,input().split())
MOD=10**9+7

frac=[0]*max(N+1,M+1)
frac[0]=1

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

for i in range(1,max(N+1,M+1)):
	frac[i]=(frac[i-1]*i)%MOD
	
if N==M:
	print((2*frac[N]*frac[M])%MOD)
else:
	print((frac[N]*frac[M])%MOD)

Solution (Python):


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

Solution (Python):
N,M=map(int,input().split())
if abs(N-M)>1:
    print(0)
    quit()
if abs(N-M)==1:
    a=1
else:
    a=2
MOD=10**9+7
def factorials(M):
    INF=int(1e9+7)
    fac=[0]*(M+1)
    finv=[0]*(M+1)
    inv=[0]*(M+1)
    fac[0]=fac[1]=1
    finv[0]=finv[1]=1
    inv[1]=1
    for i in range(2,M+1):
        fac[i]=(fac[i-1]*i)%INF
        inv[i]=INF-(inv[INF%i]*(INF//i))%INF
        finv[i]=finv[i-1]*inv[i]%INF
    return fac,finv
def nCr(n,r):
    INF=int(1e9+7)
    if n<r or r<0 or n<0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%INF)%INF

fac,finv=factorials(max(N,M))

print((a*fac[N]*fac[M])%MOD)

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

mod = 10**9+7
import math
if N != M:
    tmp = math.factorial(min(N,M))
    print(tmp**2 * max(N,M) % mod)
else:
    tmp = math.factorial(N)
    print(tmp**2 * 2 % mod)

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

fact = [1]
for i in range(1, N+M+1):
  fact.append((fact[-1] * i) % MOD)

x = fact[N] * fact[M]

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

print(answer)

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

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

Solution (Python):
import math

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

if max(N, M) > min(N, M) + 1:
  answer = 0
elif N == M: 
  answer = math.factorial(N) * math.factorial(M) * 2
elif max(N, M) == min(N, M) + 1:
  answer = math.factorial(N) * math.factorial(M)

print(answer % MOD)

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

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

Solution (Python):
from math import factorial
mod = 1000000007
n,m = (int(i) for i in input().split())
if abs(n-m) >= 2:
    print(0)
    exit()
if n == m:
    ans = factorial(n)*factorial(m)*2
    print(ans%mod)
else:
    ans = factorial(n)*factorial(m)
    print(ans%mod)

Solution (Python):
nm = input().split(" ");
n = int(nm[0]);
m = int(nm[1]);
mod = 10**9+7
if n - m == 1 or m - n == 1:
	ansn = 1;
	for i in range(n) :
		ansn = (ansn % mod)*((n-i) % mod);
		ansm = 1;
	for j in range(m) :
	 	ansm = (ansm % mod)*((m-j) % mod);
	print((ansn*ansm) % mod);
elif n == m :
	ansn = 1;
	for i in range(n) :
		ansn = (ansn % mod)*((n-i) % mod);
		ansm = 1;
	for j in range(m) :
  		ansm = (ansm % mod)*((m-j) % mod);
	print((ansn*ansm*2) % mod);
else :
	print(0);

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

MOD = 10**9 + 7
gap = M - N
if gap >= 2:
	ans = 0
else:
	ans = f(M) * f(N)
	if gap == 0:
		ans *= 2 % MOD
print(ans % MOD)

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

gap = M - N
if gap >= 2:
	ans = 0
else:
	ans = f(M) * f(N)
	if gap == 0:
		ans *= 2
print(ans%(10**9+7))

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

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