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())
if abs(n - m) > 1:
    print(0)
else:
    ret1 = 1
    for i in range(1, n + 1):
        ret1 = (ret1 * i) % 1000000007
    ret2 = 1
    for i in range(1, m + 1):
        ret2 = (ret2 * i) % 1000000007
    print((ret1 * ret2 * (2 if n == m else 1)) % 1000000007)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
"""def fac(n):
	ret = 1
	for i in range(2,n+1):
		ret = (ret*i) % (10**9+7)
    return ret"""
def fac(n):
    ret = 1
    for i in range(2,n+1):
        ret = ret*i % (10**9+7)
    return ret
a,b = map(int,input().split())
mod = 10**9+7
if abs(a-b) >= 2:
	print(0)
elif abs(a-b) == 1:
	print(fac(a)*fac(b)%mod)
else:
	print(2*fac(a)*fac(b)%mod)

Submission status: Accepted

Solution code:
import sys
import math

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

Submission status: Accepted

Solution code:
import math

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


M = math.factorial(m)
N = math.factorial(n)


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

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

elif n != m:
    print(N * M % MOD)

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())
ma = max(N,M)
mi = min(N,M)

ans = 0
if (ma - mi == 1):
  ans = math.factorial(ma) * math.factorial(mi)
elif (ma - mi == 0):
  ans = math.factorial(ma) * math.factorial(mi) * 2
print(ans % (10 ** 9 + 7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def reconclied(n , m):

    ans = 0
    mod = 10**9 + 7

    if n == m:
        ans = 2 * (math.factorial(n) * math.factorial(m))

    elif abs(n - m) == 1:
        ans = (math.factorial(n) * math.factorial(m))

    return ans % mod

def main():
    n , m = map(int , input().split())
    print(reconclied(n , m))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split()]
k = max(N,M)
l = min(N,M)
N = k
M = l
mod = 10**9+7
def frac(N):
    ans = 1
    ans1 = 1
    ans2 = 1
    ans3 = 1
    for i in range(2,N+1):
        if i % 3 == 0:
            ans1 *=i
            ans1 %= mod
        if i % 3 == 1:
            ans2 *=i
            ans2 %= mod
        if i % 3 == 2:
            ans3 *=i
            ans3 %= mod
    ans = ans1*ans2*ans3 % mod
    return ans
if N - M > 1:
    print(0)
    exit()
if N == M:
    F = frac(N)
    f = F * F % mod
    print((2*f) % mod)
    exit()
if N == M+1:
    F1 = frac(M)
    F2 = frac(N)
    print((F1*F2) % mod)
    exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
md=10**9+7
def P(m):
  ret=1
  for i in range(1,m+1):
    ret = (ret*i)%md
  return ret

if N<M:
  N,M=M,N
MK = P(M)
if N==M:
  ans=(2*(MK**2))%md
elif N==M+1:
  ans=(MK**2)%md
  ans=(ans*N)%md
else:
  ans=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

if abs(N - M) >= 2:
  print(0)
else:
  if N == M:
    print((permutation(N) * permutation(M) * 2) % MOD)
  else:
    N, M = max(N, M), min(N, M)
    print((permutation(N) * permutation(M)) % MOD)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
#### 逆元リストいらない
fact = [1]
for i in range(10**5+5):
    new_fact = fact[-1]*(i+1)%mod
    fact.append(new_fact)
N,M = map(int,input().split())
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = fact[N] * fact[M] % mod
elif abs(N-M) == 0:
    ans = fact[N] * fact[M] * 2 % mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
N, M = map(int, input().split())
mod = 10 ** 9 + 7
ans = 1
if abs(N - M) > 1:
    print(0)
    exit()

L = sorted([N, M])

a2, a1 = L
if a1 == a2:
    for i in range(a1):
        ans *= (a1 - i)**2
        ans %= mod
        # print("ans", ans)
    print((ans*2) % mod)
else:
    for i in range(a2):
        ans *= a2 - i
        ans %= mod
        ans *= a2 - i
        ans %= mod
        # print("ans", ans)
    ans *= a1
    print(ans % mod)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,M=map(int,input().split())
ans=1
MOD=10**9+7
for i in range(1,N+1):
    ans*=i
    ans=ans%MOD
for j in range(1,M+1):
    ans*=j
    ans=ans%MOD

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

Submission status: Accepted

Solution code:
# coding: utf-8
N,M=map(int,input().split())
MOD=10**9+7

if abs(N-M)==0:
    p=2
elif abs(N-M)==1:
    p=1
else:
    p=0

P=[1]

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

ans=p*P[N]*P[M]%MOD
print(ans)

Submission status: Accepted

Solution code:
def kaijo(x):
    v=1
    for i in range(1,x+1):
        v*=i
    return v


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

print(ans)

Submission status: Accepted

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

diff=abs(n-m)
if diff < -1 or diff > 1:
    print(0)
    exit()

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

from math import factorial
res =  factmod(n,7+10**9)*factmod(m,7+10**9)
if diff==0:
    res*=2
res = res%(7+10**9)
print(res)

Submission status: Accepted

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

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

	print(ans % MOD)

Submission status: Accepted

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

if abs(n-m) <= 1:
  sumN = 1
  sumM = 1 
  for i in range(2,n+1):
    sumN = sumN*i % (10**9+7) 
  for i in range(2,m+1):
    sumM = sumM*i % (10**9+7) 
  if n==m:
    print(2*sumM*sumN % (10**9+7))
  else:
    print(sumM*sumN % (10**9+7))
else:
  print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ans = 0
d = [0] * (max([n,m])+1)
def factorial(n):
    global d
    d[0] = 1
    for i in range(1, n+1):
        d[i] = ((i+1) * d[i-1]) % (10** 9 +7)
factorial(max(m,n))
if abs(n-m) == 1:
    ans = d[n-1] * d[m-1]
elif n == m:
    ans = 2 * d[n-1] * d[m-1]
print(ans % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
import math

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

d = 10 ** 9 + 7

fn = math.factorial(n) % d
fm = math.factorial(m) % d

if abs(n - m) >= 2:
	print(0)
elif abs(n - m) == 1:
	print((fn * fm) % d)
elif abs(n - m) == 0:
	print((2 * fn * fm) % d)

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
a = 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

else:
    ans=int(1)
    for i in range(1,n+1):
        ans*=i
        ans%=1000000007
    
    for i in range(1,m+1):
        ans*=i
        ans%=1000000007

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

Submission status: Accepted

Solution code:
import math
MOD = 1000000007
m,n=map(int,input().split())
f=abs(m-n)
if f == 0:
  ans=2*math.factorial(m)*math.factorial(n)%MOD
  print(ans)
  exit()
elif f == 1:
  ans=math.factorial(m)*math.factorial(n)%MOD
  print(ans)
  exit()
else:
  print(0)
  exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

if abs(n-m)>1:
  print(0)
elif n==m:
  print(2*pow(factorial(n), 2, mod_n)%mod_n)
else:
  print(factorial(n)*factorial(m)%mod_n)

Submission status: Accepted

Solution code:
# 2020/01/25
# AtCoder Beginner Contest 065 - C

import math

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

# Calc
sub = abs(n - m)

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

# Output
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

input = sys.stdin.readline


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

    MOD = 10 ** 9 + 7

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

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



    
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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


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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
def perm(n):
    a=1
    for i in range(1,n+1):
        a*=i*i
        a%=10**9+7
    return a

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

X = 10 ** 9 + 7
ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

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

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

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

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

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

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

Submission status: Accepted

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

print(answer % mod)

Submission status: Accepted

Solution code:
# ABC065c
import sys
import math
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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


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

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

Solution code:
MAX = 100003 #階乗のmodを求めたい中で最大のもの
MOD = 10**9+7

fac = [0]*MAX   #階乗のmod
inv = [0]*MAX    #その数の逆元
finv = [0]*MAX   #階乗の逆元

def COMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        fac[i] = (fac[i-1] * i) % MOD
        inv[i] = MOD-(inv[MOD%i] * (MOD//i))%MOD
        finv[i] = (finv[i-1]*inv[i]) %MOD
        
    
def COM(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return (fac[n] * (finv[k]*finv[n-k]))%MOD

#pが素数でないと使えない
COMinit()    
n,m = map(int,input().split())
if abs(n-m) > 1:
  print(0)
elif abs(n-m) == 1:
  print(fac[n]*fac[m] %MOD)
else:
  print(2*fac[n]*fac[m] %MOD)

Submission status: Accepted

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

Submission status: Accepted

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

from math import factorial

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

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

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

Submission status: Accepted

Solution code:
p = 10 ** 9 + 7
N = (10 ** 5)*2 +10  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


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

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 1

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

Submission status: Accepted

Solution code:
import itertools

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

Mod = 1000000007
Sum = 0

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

    n = 1
    m = 1

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

    Sum = (n * m) % Mod

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

    print(Sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
def lam(x,mo):
	ans=1
	for i in range(1,x+1):
		ans*=i
		ans%=mo
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n,mod)*lam(m,mod)%mod)
else:
	print(2*(lam(n,mod)**2)%mod)

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7

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

fac = make_factorial_mod(10**5, MOD)

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

def main():

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

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(ans)