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



Sample input:
2 2


Sample output:
8

Submission status: Accepted

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

mod = 10**9 + 7
l = n+m
ans = 1
b = max(n, m)
s = min(n, m)
for i in range(l):
    if i % 2 == 0:
        tmp = b-i//2
        if tmp > 0:
            ans = (ans % mod) * (tmp % mod)
        else:
            print(0)
            exit()
    else:
        tmp = s-i//2
        if tmp > 0:
            ans = (ans % mod) * (tmp % mod)
        else:
            print(0)
            exit()
if l%2 == 0:    
    ans = (ans % mod) * (2 % mod)
print(ans%mod)

Submission status: Accepted

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

p=10**9+7
min_=min(N,M)
max_=max(N,M)
ans=1
for i in range(1,min_+1):
    ans=(ans*i)%p
    ans=(ans*i)%p

if abs_NM==1:
    ans=(ans*max_)%p
else:
    ans=(ans*2)%p

print(ans)

Submission status: Accepted

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

p=10**9+7
ans=1
for i in range(1,N+1):
    ans=(ans*i)%p
for i in range(1,M+1):
    ans=(ans*i)%p
if abs_NM==0:
    ans=(ans*2)%p

print(ans)

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


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


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

if abs(N - M) < 2:
    ans += fact(N, MOD) * fact(M, MOD) % MOD
    if N == M:
        ans *= 2
        ans %= MOD
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

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

factorial=[1 for i in range(10**5+5)]
for i in range(1,10**5+5):
    if i==1:factorial[i]=1
    else:factorial[i] = factorial[i-1]*i % mod
      
if abs(N-M)>1:
  print(0)
  exit()

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

Submission status: Accepted

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

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

if abs(n-m)==1:
    ans=1
else:
    ans=2

while n>0:
    ans*=n
    ans%=mod
    n-=1

while m>0:
    ans*=m
    ans%=mod
    m-=1

print(ans)

Submission status: Accepted

Solution code:
def factrial(n):
    INF = 10 ** 9 + 7
    ret = 1
    for i in range(2, n + 1):  ret = (ret * i) % INF
    return ret

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline

MOD = 10**9 + 7

def modFact(x):
  if x < 1:
    raise ValueError
  res = 1
  for i in range(x):
    res = res * (i+1) % MOD
  return res

# @njit
def solve(n,m):
  if abs(n-m) > 1:
    return 0
  elif abs(n-m) == 1:
    return modFact(n) * modFact(m) % MOD
  else:
    return modFact(n) * modFact(m) * 2 % MOD


def main():
  N,M = map(int,input().split())
  # a = list(map(int,input().split()))
  print(solve(N,M))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
mod = 10**9+7

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def mod1(x):
  mod=10**9+7
  ans=1
  y=1
  while y<=x:
    ans=ans*y
    y+=1
    ans=ans%mod
    
  return ans%mod

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = 10**9+7

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().rstrip().split(" "))
if abs(N - M) > 1:
    print(0)
else:
    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
    if N == M:
        ans *= 2
        ans %= 10 ** 9 + 7
    print(ans)

Submission status: Accepted

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

fac = 1
num = 10**9 + 7
for i in range(1,N+1):
    fac *= i
    if fac > num:
        fac %= num

fac2 = 1
for i in range(1,M+1):
    fac2 *= i
    if fac2 > num:
        fac2 %= num

if N == M:
    print(fac**2*2%num)
else:
    print(fac*fac2%num)

Submission status: Accepted

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


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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math


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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

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

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

def calc():
    dp[1] = 1
    for i in range(2, maxmn + 1):
        dp[i] = mod(dp[i-1] * i)

absnm = abs(N - M)

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

if absnm > 1:
    print(0)

else:
    calc()

    if absnm == 1:
        r = mod(dp[M] * dp[N])

    elif absnm == 0:
        r = mod(dp[M] * dp[N] * 2)

    else:
        r = 0

    print(r)

Submission status: Accepted

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

x = abs(N-M)
if x >= 2:
    print(0)
else:
    mod = 10**9+7
    a = 1
    for i in range(2, N+1):
        a *= i
        a %= mod
    b = 1
    for i in range(2, M+1):
        b *= i
        b %= mod
    
    if x == 0:
        print(a*b*2%mod)
    else:
        print(a*b%mod)

Submission status: Accepted

Solution code:
import math
def ModFact(N,Mod):
    Num = 1
    for T in range(2,N+1):
        Num = (Num*T)%Mod
    return Num
  
N,M = (int(T) for T in input().split())
Mod = 10**9+7
if N==M:
    print((2*ModFact(N,Mod)*ModFact(M,Mod))%Mod)
elif N==M+1 or M==N+1:
    print((ModFact(N,Mod)*ModFact(M,Mod))%Mod)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int , input().split())
ans  = 1
mod = 10**9+7
for i in range(1, n+1):
	ans = (ans%mod)*(i%mod)
	ans = ans%mod
for i in range(1, m+1):
	ans = (ans%mod)*(i%mod)
	ans = ans%mod
# if the difference between them is greater than 1 they can't be arranged
if abs(n-m)>1:
	print(0)
# if they have same number they can be placed in odd position or even position independently
elif abs(n-m)==0:
	ans = ans*2
	ans = ans%mod
	print(ans)
# only one way no exchange of odd and even place 
else:
	print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class Modulo():
    def __init__(self,a,n):
        self.a=a%n
        self.n=n

    def __str__(self):
        return "{} (mod {})".format(self.a,self.n)

    #+,-
    def __pos__(self):
        return self

    def __neg__(self):
        return  Modulo(-self.a,self.n)

    #等号,不等号
    def __eq__(self,other):
        if isinstance(other,Modulo):
            return (self.a==other.a) and (self.n==other.n)
        elif isinstance(other,int):
            return (self-other).a==0

    def __neq__(self,other):
        return not(self==other)
    
    #加法
    def __add__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a+other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a+other,self.n)

    def __radd__(self,other):
        if isinstance(other,int):
            return Modulo(self.a+other,self.n)
        
    #減法
    def __sub__(self,other):
        return self+(-other)

    def __rsub__(self,other):
        if isinstance(other,int):
            return -self+other
        
    #乗法
    def __mul__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a*other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a*other,self.n)
        
    def __rmul__(self,other):
        if isinstance(other,int):
            return Modulo(self.a*other,self.n)
        
    #Modulo逆数
    def Modulo_Inverse(self):
        x0, y0, x1, y1 = 1, 0, 0, 1
        a,b=self.a,self.n
        while b != 0:
            q, a, b = a // b, b, a % b
            x0, x1 = x1, x0 - q * x1
            y0, y1 = y1, y0 - q * y1

        if a!=1:
            raise Modulo_Error("{}の逆数が存在しません".format(self))
        else:
            return Modulo(x0,self.n)

    #除法
    def __truediv__(self,other):
        return self*(other.Modulo_Inverse())
    
    def __rtruediv__(self,other):
        return other*(self.Modulo_Inverse())

    #累乗
    def __pow__(self,m):
        u=abs(m)

        r=Modulo(1,self.n)

        while u>0:
            if u%2==1:
                r*=self
            self*=self
            u=u>>1

        if m>=0:
            return r
        else:
            return r.Modulo_Inverse()

    #根号
    def sqrt(self):
        if self==0:
            return self
        elif self.n==2:
            return self
        elif self.n%4==3:
            return self**((self.n+1)//4)
        else:
            p=self.n
            u=2
            s=1
            while (p-1)%(2*u)==0:
                u*=2
                s+=1

            z=Modulo(2,p)
            while z**((p-1)//2)!=-1:
                z+=1

            q=(p-1)//u
            m=s
            c=z**q
            t=self**q
            r=self**((q+1)//2)

            while m>1:
                k=1
                d=t*t

                while d!=1:
                    k+=1
                    d*=d
                    print(m,k)
                b=Modulo(2,p)**(2**(m-k-1))
                c,t,r,m=b*b,t*b*b,r*b,k
            return r
#=======================================
N,M=map(int,input().split())
K=10**9+7
if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    N,M=max(N,M),min(N,M)
    P=Modulo(1,K)
    for i in range(1,M+1):
        P*=i
    print((P*P*N).a)
else:
    P=Modulo(1,K)
    for i in range(1,M+1):
        P*=i
    print((P*P*2).a)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9+7
"""
abs(n-m)>1の時、必ず隣り合ってしまうので0
n>mとしても一般性を失わない
if n<m: n, m = m, n
n==mnの時は、どちらを左端に置くか選べるので2倍
"""

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

Submission status: Accepted

Solution code:
from math import *
N,M = map(int,input().split())
if abs(N - M) > 1:
    print(0)
    exit(0)

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

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

def f(n):
    ret = 1
    while n > 1:
        ret *= n
        ret %= MOD
        n -= 1
    return ret

ans = 1
ans *= f(N)
ans *= f(M)
if N == M:
    ans *= 2
print(ans % MOD)

Submission status: Accepted

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

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

c = math.factorial(n) * math.factorial(m)
result = 2 * c if n == m else c
print(result % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m = map(int, input().split())
mod = 10**9 + 7
if -2 < n - m < 2:
    d = 1
    s = 1
    for i in range(1, n + 1):
        d = d * i % mod
    for i in range(1, m + 1):
        s = s * i % mod
    print((d * s * (1 + (n == m))) % mod)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    COM_MAX = max(N, M)

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

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
n,m = map(int,input().split())
max = max(n,m)
min = min(n,m)
total = n+m
f_n = math.factorial(n)
f_m = math.factorial(m)
if max - min >= 2:
	print(0)
	sys.exit()
if total%2 == 0:
	ans = f_n*f_m*2
else:
	ans = f_n*f_m
if ans > 10**9+7:
	print(ans%(10**9+7))
else:
	print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[4]:


import math


# In[17]:


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


# In[18]:


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


# In[ ]:

Submission status: Accepted

Solution code:
from math import factorial
n,m=map(int, input().split())
ans=0
if abs(n-m)==0:
    ans=factorial(n)*factorial(m)*2
elif abs(n-m)==1:
    ans=factorial(n)*factorial(m)

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

Submission status: Accepted

Solution code:
class Factorial():
    def __init__(self, mod=10**9 + 7):
        self.mod = mod
        self._factorial = [1]
        self._size = 1
        self._factorial_inv = [1]
        self._size_inv = 1

    def __call__(self, n):
        return self.fact(n)

    def fact(self, n):
        ''' n! % mod '''
        if n >= self.mod:
            return 0
        self._make(n)
        return self._factorial[n]
    
    def _make(self, n):
        if n >= self.mod:
            n = self.mod
        if self._size < n+1:
            for i in range(self._size, n+1):
                self._factorial.append(self._factorial[i-1]*i % self.mod)
            self._size = n+1

    def fact_inv(self, n):
        ''' n!^-1 % mod '''
        if n >= self.mod:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        self._make(n)
        if self._size_inv < n+1:
            self._factorial_inv += [-1] * (n+1-self._size_inv)
            self._size_inv = n+1
        if self._factorial_inv[n] == -1:
            self._factorial_inv[n] = self.modinv(self._factorial[n])
        return self._factorial_inv[n]
    
    @staticmethod
    def xgcd(a, b):
        '''
        Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)
        '''
        x0, x1, y0, y1 = 0, 1, 1, 0
        while a != 0:
            (q, a), b = divmod(b, a), a
            y0, y1 = y1, y0 - q * y1
            x0, x1 = x1, x0 - q * x1
        return b, x0, y0

    def modinv(self, n):
        g, x, _ = self.xgcd(n, self.mod)
        if g != 1:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        return x % self.mod

    def comb(self, n, r):
        ''' nCr % mod '''
        if r > n:
            return 0
        t = self(n)*self.fact_inv(n-r) % self.mod
        return t*self.fact_inv(r) % self.mod
    
    def comb_(self, n, r):
        '''
        nCr % mod
        when r is not large and n is too large
        '''
        c = 1
        for i in range(1, r+1):
            c *= (n-i+1) * self.fact_inv(i)
            c %= self.mod
        return c

    def comb_with_repetition(self, n, r):
        ''' nHr % mod '''
        t = self(n+r-1)*self.fact_inv(n-1) % self.mod
        return t*self.fact_inv(r) % self.mod

    def perm(self, n, r):
        ''' nPr % mod '''
        if r > n:
            return 0
        return self(n)*self.fact_inv(n-r) % self.mod

n, m = map(int, open(0).read().split())
if abs(n-m) > 1:
    print(0)
else:
    mod = 10**9 + 7
    fact = Factorial()
    t = fact(n) * fact(m) % mod
    if n == m:
        print(2*t % mod)
    else:
    	print(t)

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10 ** 9 + 7

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

    if abs(n - m) == 1:
        print(res)
    else:
        print(res * 2 % mod)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
# C - Reconciled?
import math
N, M = map(int,input().split())

S = math.factorial(min(N,M)) % 1000000007

ans = 0
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = S*S*max(N,M)
else:
    ans = S*S*2

ans = ans % 1000000007

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if abs(N-M)==0:
  ans = 2*factorial(N)*factorial(M)
elif abs(N-M)==1:
  ans = factorial(N)*factorial(M)
else:
  ans = 0

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')

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

def solve():
    n, m = [int(x) for x in input().split()]
    n, m = min(n,m), max(n,m)
    x = m - n
    if x > 1:
        print(0)
    elif x == 1:
        print((fmod(n) * fmod(m))%MOD)
    else:
        print((fmod(n) * fmod(m)*2)%MOD)


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())
R = 1000000007

if abs(n - m) >= 2:
    ans = 0
else:
    nf = math.factorial(n)
    mf = math.factorial(m)
    if n == m:
        ans = ((nf % R) * (mf % R) * 2) % R
    else:
        ans = (nf * mf) % R

print(ans)

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())
R = 1000000007

if abs(n - m) >= 2:
    ans = 0
else:
    nf = math.factorial(n)
    mf = math.factorial(m)
    if n == m:
        ans = (nf * mf * 2) % R
    else:
        ans = (nf * mf) % R

print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial
n, m = [int(x) for x in input().split()]
temp = abs(n - m)
if temp > 1:
    print(0)
    exit()
mod = 10 ** 9 + 7
f_n = factorial(n) % mod
f_m = factorial(m) % mod
ans = f_n * f_m % mod
if temp == 0:
    ans = ans * 2 % mod
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ans = 0

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


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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

Submission status: Accepted

Solution code:
from math import factorial


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
MOD = 1000000007
def nPk(n, k):
    r = 1
    while k > 0:
        r *= n
        r %= MOD
        n -= 1
        k -= 1
    return r
if abs(n - m) > 1:
    print(0)
elif n == m:
    print(nPk(n, n) * nPk(m, m) * 2 % MOD)
else:
    print(nPk(n, n) * nPk(m, m) % MOD)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

n = 100010
frac = [1]*(n+1)
for i in range(n):
    frac[i+1] = (i+1)*frac[i]%mod
if N==M+1:
    print(frac[N]*frac[M]%mod)
elif N==M:
    print(frac[N]*frac[M]*2%mod)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
a, b = [int(i) for i in input().split()]
if abs(a-b) > 1:
    print(0)
elif a == b:
    print((2 * factorial(a) * factorial(b)) % (10 ** 9 + 7))
else:
    print((factorial(max(a, b)) * factorial(min(a, b))) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
a,b=sorted(map(int,input().split()))
mod=10**9+7
ans=1
if a==b:
    for i in range(1,a+1):
            ans=ans*i*i%mod
    ans=ans*2%mod        
elif b-a>1:ans=0
else:
    for i in range(1,a+1):
        ans=ans*i*i%mod
    ans=ans*b%mod
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
from math import factorial as f

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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
if abs(n-m)>=2:
    print(0)
    exit()
mod=1000000007
if n==m:
    nf=1
    mf=1
    for i in range(n):
        nf=nf*(n-i)%mod
        mf=mf*(n-i)%mod
    print(int(2*nf*mf%mod))
else:
    if m>n:
        n,m=m,n
    nf=1
    mf=1
    for i in range(n):
        nf=nf*(n-i)%mod
        if i!=n-1:mf=mf*(m-i)%mod
    print(int(nf*mf%mod))