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

Solution (Python):
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict 
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  
  
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  






a,b=map(int,input().split())
if abs(a-b)>=2:
    print(0)
    exit()
mod=10**9+7
re=2 if a==b else 1
def kaijou(n):
    if n==1:return 1
    else:
        return (kaijou(n-1)*n)%mod


print(kaijou(a)*kaijou(b)*re%mod)

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

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

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

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

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

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(math.factorial(N)*math.factorial(M)*2%MOD)

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 = ans * i % MOD

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

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

Solution (Python):
SIZE=2*10**5; MOD=10**9+7 

SIZE += 1
fac = [0]*SIZE  
fac[0] = fac[1] = 1
for i in range(2,SIZE):
    fac[i] = fac[i-1]*i%MOD



import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

if abs(n-m) >= 2:
    print(0)
elif abs(n-m)==1:
    print(fac[n]*fac[m]%MOD)
elif n==m:
    print(2*fac[n]*fac[m]%MOD)

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

fact = [1]
for i in range(1, max(n, m) + 1):
    fact.append(fact[-1] * i % mod)

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

Solution (Python):
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
  exit()
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((factorial(m)*factorial(n))%mod)
  exit()
x = factorial(n)
print((2*x*x)%mod)

Solution (Python):
n,m = map(int,input().split())
lim = int(1e+9+7)


def pat(r,t = lim):
    ans = 1
    for i in range(r):
        ans = (ans*(i+1))%t
    return ans%t

a = pat(n)
b = pat(m)

if abs(n-m) < 1:
    print((a*b*2)%lim)
elif abs(n-m) == 1:
    print((a*b)%lim)
else:
    print(0)

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

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


def cumprod(L, mod=mod):
    A = np.arange(L, dtype=np.int64)
    A[0] = 1
    Lsq = int(L**.5 + 1)
    A = np.resize(A, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        A[:, n] *= A[:, n - 1]
        A[:, n] %= mod
    for n in range(1, Lsq):
        A[n] *= A[n - 1, -1]
        A[n] %= mod
    return A.ravel()[:L]


fact = cumprod(2 * 10**6 + 10, mod)
res = fact[n] * fact[m]
if n == m:
  res *= 2
print(res % mod)

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

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

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

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

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

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

if abs(n-m)>=2:
    print(0)
elif n==m:
    print(2*fac(n)**2%INF)
else:
    print(fac(min(n,m))**2*max(n,m)%INF)

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

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

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

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

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

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

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

def comb(a,p):
    ini=1
    for i in range(1,a+1):
        ini = (ini*i)%p
    return ini


if abs(N-M)>1:
    ans=0
else:
    if N!=M:
        k=1
    else:
        k=2
       
    dog=comb(N,p)
    mon=comb(M,p)
    tot=comb(N+M,p)
   
    ans= (k*dog*mon)%p
   
print(ans)

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


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
mod=10**9+7
n,m=map(int,input().split())
F=[0]*(10**5+1)
F[0]=1
for i in range(1,10**5+1):
  F[i]=(F[i-1]*i)%mod

if abs(n-m)>1:
  print(0)
else:
  ans=(F[n]*F[m])%mod
  if n==m:
    ans=(ans*2)%mod
  print(ans)

Solution (Python):

mod=10**9+7
N=100005
kaidan=[1 for i in range(N)]
gyaku=[1 for i in range(N)]
for i in range(1,N):
    kaidan[i] = (i * kaidan[i-1])%mod
    gyaku[i]=pow(kaidan[i], mod-2, mod)
def comb(a,b):
    return kaidan[a]*gyaku[b]*gyaku[a-b]
n,m=map(int,input().split())
if abs(n-m)>=2:
    print(0)
else:
    if n==m:
        print(kaidan[n]*kaidan[n]*2%mod)
    else:
        a=max(n,m)
        b=min(n,m)
        print(kaidan[a]*kaidan[b]%mod)

Solution (Python):
from math import factorial
from sys import stdin


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


def main():
    N, M = [int(x) for x in stdin.readline().rstrip().split()]
    if N == M:
        x = (factorial(N) % (10 ** 9 + 7)) * (factorial(M) % (10 ** 9 + 7))
        print((x * 2) % (10 ** 9 + 7))
    elif abs(N - M) == 1:
        x = (factorial(N) % (10 ** 9 + 7)) * (factorial(M) % (10 ** 9 + 7))
        print(x % (10 ** 9 + 7))
    else:
        print(0)


if __name__ == "__main__":
    main()

Solution (Python):


mod = 10**9+7
"""
def chs(k,i,mod):
    deno = 1
    for j in range(k,k-i,-1):
        deno*=j
        deno%=mod
    nume = 1
    for j in range(1,j+1):
        nume*=pow(j,mod-2,mod)
        nume%=mod
    return deno*nume%mod
"""

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

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache

class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def map_int():
        return [int(x) for x in Scanner.string().split()]
    def string_list(n):
        return [input() for i in range(n)]
    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]
    def int_cols_list(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True

MOD = int(1e09) + 7

def main():
    N,M = Scanner.map_int()
    if not abs(N - M) <= 1:
        print(0)
        return
    
    n ,m = 1,1
    for i in range(2,N + 1):
        n *= i
        n%=MOD
    for i in range(2,M + 1):
        m *= i
        m %= MOD
    ans = 0
    if N == M:
        ans = (n * m) % MOD * 2 % MOD
    else:
        ans = n * m % MOD
    print(ans)
    return

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from functools import reduce,lru_cache
import math
from operator import mul

def main():
  
  N,M = map(int,input().split())
  
  @lru_cache(maxsize = None)
  def recur(n):
    if n == 0:
      return 1
    else:
      m = n * recur(n - 1)
      return m
      
  if N == M:
    L = [i for i in range(1,N + 1)]
    
    ans = (math.factorial(M) ** 2) * 2 % ((10 ** 9) + 7)
    print(ans)
  elif abs(M - N) == 1: 
    ans = math.factorial(N) * math.factorial(M) % ((10 ** 9) + 7)
    print(ans)
    
  else:
    print(0)
    
main()

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

mod = 10**9+7

x = max(N, M)
y = min(N, M)
if x-y <= 1:
    ans = 1
    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
else:
    ans = 0
print(ans)

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

fact_n = math.factorial(n)
fact_m = math.factorial(m)

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

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

Solution (Python):

mod=10**9+7
n,m=map(int,input().split())
def find_power(n):
    
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist
p=find_power(10**5+4)
if abs(n-m)>1:
    print(0)
elif n==m:
    print(p[n]*p[m]*2%mod)
else:
    print(p[n]*p[m]%mod)

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

if M == N:
    print(m*n*2 % MOD)
else:
    print(m*n % MOD)

Solution (Python):
N, M = map(int, input().split(' '))
import math
n, m = int(math.factorial(N)), int(math.factorial(M))
l = int(math.pow(10,9)) + 7

if N == M:
    n *= m * 2
    n %= l
    print(n)

elif N + 1 == M:
    n *= m
    n %= l
    print(n)

elif N == M + 1:
    n *= m
    n %= l
    print(n)

else:
    print(0)

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

Solution (Python):
from math import factorial as fc

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

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

Solution (Python):
from math import factorial

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

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

ex = [1]
mod = 10**9+7
for i in range(1,10**5+1):
    ex.append((ex[-1]*i)%mod)
    
if abs(N-M) >= 2:
    print(0)
elif N==M:
    print((2*ex[N]**2)%mod)
else:
    print((ex[N]*ex[M])%mod)

Solution (Python):
import math

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

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

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

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

Solution (Python):
import math

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

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

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

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

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


def main():
    n, m = map(int, input().split())
    if abs(n - m) > 1:
        return 0


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



    if n == m:
        return fact ** 2 * 2 % mod
    else:
        return fact * fact * (min(n, m) + 1) % mod




print(main())

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

def fac(n):
    r = [1]*(n+1)
    for i in range(1, n+1):
        r[i] = r[i-1] * i % ppp
    return r[n]

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

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

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

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

Solution (Python):


import sys
import math



tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

n=hoge[0]
m=hoge[1]





t = math.factorial(min(n,m))


if(abs(n-m)>1):
	print(0)
elif(n==m):
	print((2*(t**2))%1000000007)
else:
	print(((t**2)*max(n,m))%1000000007)

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

Solution (Python):
N, M = list(map(int,input().split()))
if abs(N - M) >= 2:
    print(0)
else:
    ans = 1
    if N == M:
        ans = 2
    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)

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

Solution (Python):
n,m=map(int,input().split())
if n>m:
	n,m=m,n
if n==m:
	ans=2
elif n==m-1:
	ans=1
else:
	ans=0
mod=10**9+7
if ans!=0:
	for i in range(1,n+1):
		ans=(ans*i)%mod
	for i in range(1,m+1):
		ans=(ans*i)%mod
	print(ans)
else:
	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) % (pow(10, 9) + 7))
else:
    print(math.factorial(N) * math.factorial(M) * 2 % (pow(10, 9) + 7))

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

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

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

Solution (Python):

from math import factorial


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(n) ** 2 * 2) % MOD  
        else:  
            ans = (factorial(n) ** 2 * m) % MOD  
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def factorial_mod(x):
    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):

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:
        prod = 1
        for i in range(1, n + 1):  
            prod = (prod * i) % MOD

        if dif == 0:  
            ans = (prod ** 2 * 2) % MOD
        else:  
            ans = (prod ** 2 * m) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from math import factorial


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


if __name__ == "__main__":
    main()

Solution (Python):
class Combinatorics:
    def __init__(self, N, mod):
        '''
        Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
        over the finite field Z/(mod)Z.
        Input:
            N (int): maximum n
            mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
        '''
        self.mod = mod
        self.fact = {i: None for i in range(N+1)}         
        self.inverse = {i: None for i in range(1, N+1)}   
        self.fact_inverse = {i: None for i in range(N+1)} 
        
        
        self.fact[0] = self.fact[1] = 1
        self.fact_inverse[0] = self.fact_inverse[1] = 1
        self.inverse[1] = 1
        for i in range(2, N+1):
            self.fact[i] = i * self.fact[i-1] % self.mod
            q, r = divmod(self.mod, i)
            self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
            self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
    
    def perm(self, n, r):
        '''
        Calculate nPr = n! / (n-r)! % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
    
    def binom(self, n, r):
        '''
        Calculate nCr = n! /(r! (n-r)!) % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
        
    def hom(self, n, r):
        '''
        Calculate nHr = {n+r-1}Cr % mod.
        Assign r objects to one of n classes.
        Arrangement of r circles and n-1 partitions:
            o o o | o o | | | o | | | o o | | o
        '''
        if n == 0 and r > 0:
            return 0
        if n >= 0 and r == 0:
            return 1
        return self.binom(n + r - 1, r)

N, M = map(int, input().split())
MOD = 10**9 + 7
if abs(N-M) > 1:
    print(0)
else:
    com = Combinatorics(max(N, M), MOD)
    if N != M:
        ans = (com.fact[N] * com.fact[M]) % MOD
    else:
        ans = (2 * (com.fact[N] * com.fact[M]) % MOD) % MOD
    print(ans)

Solution (Python):
import math

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


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

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

    if N == M:
        print(fact[N] * fact[M] * 2 % MOD)
    elif N + 1 == M:
        print(fact[N] * fact[M] % MOD)

Solution (Python):
import math

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

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

Solution (Python):
import math
N,M=map(int,input().split(' '))
if abs(N-M) not in (0,1):
  print(0)
else:
  x = math.factorial(N) % (10**9+7)
  y = math.factorial(M) % (10**9+7)
  print(((2 - abs(N-M))*x*y)%(10**9+7))

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

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

mod = 10**9+7 
P = 10**5
g1 = [1, 1] 

for i in range( 2, P + 1 ):
    g1.append( ( g1[-1] * i ) % mod )

if abs(N - M) == 0:
    print(g1[N] * g1[M] * 2 % mod)
elif abs(N - M) == 1:
    print(g1[N] * g1[M] % mod)
else:
    print(0)

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

if N==M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= 10**9 + 7
  ans *= ans
  ans %= 10**9 + 7
  ans *= 2
  ans %= 10**9 + 7
elif N==M+1 or N+1==M:
  ans = 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:
  ans = 0
print(ans)

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

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

Solution (Python):
MAX_REMAINDER = 10 ** 9 + 7
N, M = map(int, raw_input().split())
if abs(N - M) >= 2:
  print 0
elif abs(N - M) == 1:
  print reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(N))) \
    * reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(M))) \
    % MAX_REMAINDER
else:
  print (reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(N))) \
    * reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(M)))) \
    * 2 \
    % MAX_REMAINDER

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


ans=1 if(abs(N-M)<2) else 0
n=max(N,M)

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

Solution (Python):


"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""



import math


def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def dmp(x):
    global debug
    if debug:
        print(x)


def probC():
    N, M = getIntList()
    N1097 = 10**9 + 7
    dmp((N, M))
    if N == M:
        times = 2
    elif abs(N-M) == 1:
        times = 1
    else:
        times = 0
    dmp(times)
    
    count = math.factorial(N) * math.factorial(M) * times % N1097
    return count


debug = False
print(probC())

Solution (Python):
import math
INF, MOD = float("inf"), 10 ** 9 + 7
MAX, MIN = -INF, INF
dx1, dy1, dx2, dy2 = [-1, 0, 1, 0], [0, -1, 0, 1], [-1, 0, 1, -1, 1, -1, 0, 1], [-1, -1, -1, 0, 0, 1, 1, 1]

def get_int():
    return int(input())

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

def mins(x, y):
    x = min(x, y)

def maxs(x, y):
    x = max(x, y)

def fact(n):
    tmp = 1
    for i in range(2, n + 1):
        tmp = int(tmp * i % MOD)
    return tmp

while(True):
    try:
        n, m = get_int_list()
        if(n == m):
            print(int(fact(n) ** 2 * 2 % MOD))
        elif(abs(n - m) == 1):
            print(int(fact(n) * fact(m) % MOD))
        else:
            print(0)
    except EOFError:
        exit()

Solution (Python):
class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]


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

mod = 10**9+7
ans = 0
F = FactMod(max(N, M), mod)

if abs(N-M) == 1:
    ans = F.fact(N)*F.fact(M) % mod
elif N == M:
    ans = F.fact(N)*F.fact(N)*2 % mod
else:
    ans = 0

print(ans)

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

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

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

print(ans)

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

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

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

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

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

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

elif N == M:
    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((2 * ans) % (10 ** 9 + 7))
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))

Solution (Python):
mod = 1000000007
n,m = map(int, input().split())
ans = 1 if abs(n-m) == 1 else 2
if abs(n-m) > 1:
    print(0)
    quit()
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):
def inpl(): return [int(i) for i in input().split()]
N, M = inpl()
mod = 10**9+7
fac = [1 for _ in range(N+3)]
for i in range(N+2):
    fac[i+1] = (i+1)*fac[i]%mod

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

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

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

Solution (Python):
import math

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