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

ans = 1

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

print(ans)

Submission status: Accepted

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

if abs(n-m)>1:
    print(0)
    exit()
elif n == m:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = 2 * ans1 * ans2 
else:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = ans2*ans1

print(ans % div)

Submission status: Accepted

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

if abs(n-m)>1:
    print(0)
    exit()
elif n == m:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = 2 * ans1 * ans2 
else:
    ma = max(n,m)
    mi = min(n,m)
    ans1 = math.factorial(ma)
    ans2 = math.factorial(mi)
    ans = ans2*ans1

print(ans % div)

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())
if N == M:
    ans = (math.factorial(N) ** 2) * 2
elif abs(N - M) == 1:
    x = max(N, M)
    ans = (math.factorial(x) ** 2) // x
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) > 1:
    print(0)
elif n == m:
    ans = 1
    for i in range(2, n + 1):
        ans *= i
        ans %= MOD
    # xoxoxo... とoxoxox... 、どっちを先にするかで2種類
    print(2 * ans * ans % MOD)
else:
    ans = 1
    for i in range(2, n + 1):
        ans *= i
        ans %= MOD
    for i in range(2, m + 1):
        ans *= i
        ans %= MOD
    print(ans)

Submission status: Accepted

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

mx = max(n, m)
mn = min(n, m)

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

Submission status: Accepted

Solution code:
def factorial(x, mod):
    ret = 1
    while x > 0:
        ret = (ret * x) % mod
        x -= 1
    return ret


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

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

Submission status: Accepted

Solution code:
from functools import reduce

def fac(n, p):
    return reduce(lambda x, y: x * y % p, range(1, n + 1))

N, M = [int(x) for x in input().split()]
p = 10 ** 9 + 7
if abs(N - M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * fac(N, p) * fac(M, p) % p
else:
    ans = fac(N, p) * fac(M, p) % p

print(ans)

Submission status: Accepted

Solution code:
def fac(n, p):
    ret = 1
    for i in range(2, n + 1):
        ret = ret * i % p
    return ret

N, M = [int(x) for x in input().split()]
p = 10 ** 9 + 7
if abs(N - M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * fac(N, p) * fac(M, p) % p
else:
    ans = fac(N, p) * fac(M, p) % p

print(ans)

Submission status: Accepted

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

def factorial(num):
    x=1
    for i in range(num,0,-1):
        x = x *i % mod

    return x
        
def permutations(num,r):
    x=1
    cnt=0
    for i in range(num,0,-1):
        if cnt>r:
            break

        x = x*i %mod
        cnt+=1

    return x

def solve():
    if abs(m-n)>1:
        print(0)
        return
    
    dog=factorial(n)

    if n==m:
        monkey=permutations(m,m-1)*2 % mod
    else:
        monkey=factorial(m)
    
    ans=dog * monkey % mod
    print(ans)
    
if __name__ == "__main__":
     solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N, M = mi()
    fact = [1]
    for i in range(1, max(N, M)+3):
        fact.append((i*fact[-1])%MOD)
    if abs(N-M) == 1:
        print((fact[N]*fact[M])%MOD)
    elif N == M:
        print((2*fact[N]*fact[M])%MOD)
    else:
        print(0)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")

n,m = I()

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

if abs(n-m) == 1:
    print(math.factorial(n)*math.factorial(m) % mod)
    sys.exit()

print(math.factorial(n)*math.factorial(m)*2 % mod)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import math

MOD = 10 ** 9 + 7
n, m = list(map(int, input().split()))
ptn = 1
if abs(n - m) >= 2:
    ptn = 0
else:
    ptn = math.factorial(n) * math.factorial(m)
    if abs(n - m) == 0:
        ptn *= 2

print(ptn % MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def comb(n,k):
    nCk = 1
    MOD = 10**9+7

    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk
n,m=map(int,input().split())
if m>n:
    n,m=m,n
N,M=1,1
mod=10**9+7
for i in range(1,n+1):
    N*=i
    N%=mod
for i in range(1,m+1):
    M*=i
    M%=mod
if n>=m+2:
    print(0)
    exit()
if n==m+1:
    ans=1
if n==m:
    ans=2
ans*=N*M
print(ans%mod)

Submission status: Accepted

Solution code:
def factorial_mod(n,mod):
    ret = 1
    for i in range(1,n+1):
        ret *= i
        ret %= mod
    return ret
def comb_mod(n,r,mod):
    if r > n or r < 0:
        ret = 0
    else:
        fact_n  = factorial_mod(n, mod)
        fact_r  = factorial_mod(r, mod)
        fact_nr = factorial_mod(n-r, mod)
        ret = fact_n * pow(fact_r, mod-2, mod) * pow(fact_nr, mod-2, mod) % mod
    return ret
n,m = map(int,input().split())
c = abs(n-m)
mod = 10**9+7
if c >= 2:
    ans = 0
elif c == 1:
    ans = factorial_mod(n,mod)*factorial_mod(m,mod)
else:
    ans = 2*factorial_mod(n,mod)*factorial_mod(m,mod)
print(ans%mod)

Submission status: Accepted

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

if abs(a-b)>=2:
    print(0)
    exit()

ans=1
for i in range(2,a+1):
    ans*=i
    ans%=mod

for i in range(2,b+1):
    ans*=i
    ans%=mod

if a==b:
    ans*=2
    ans%=mod

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000000)

mod = 1000000007


def fac(n):
    v = 1
    for i in range(n):
        v = int(((i+1) * v) % mod)
    return v


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
twice=(N==M)+(abs(N-M)<=1)

n=m=1
while N:
    n*=N
    n%=10**9+7
    N-=1

while M:
    m*=M
    m%=10**9+7
    M-=1

print((n*m*twice)%(10**9+7))

Submission status: Accepted

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
tmp = 1

for i in range(1,min(n,m)+1):
    tmp = tmp * i % (10**9+7)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline

N,M = list(map(int,input().split()))
big = max(N,M)
small = min(N,M)
m=1000000007

if big == small:
    print(math.factorial(big)**2*2%m)
elif big == small + 1:
    print(math.factorial(big)*math.factorial(small)%m)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def s(n,m):
  f = 1
  for i in range(1,n+1):f*=i;f%=m
  return f
N,M=map(int,input().split());m=10**9+7
print(s(N,m)*s(M,m)*(2-(N+M)%2)%m if abs(N-M) < 2 else 0)

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')
MOD = 10 ** 9 + 7


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

ans = 1
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:
    ans = ans * 2 % MOD
print(ans % MOD)

Submission status: Accepted

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

NN = math.factorial(N) % MOD
MM = math.factorial(M) % MOD
if N == M:
    print(2 * NN * MM % MOD)
else:
    print(NN * MM % MOD)

Submission status: Accepted

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


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

if abs(n - m) >= 2:
    print(0)
else:
    if n == m:
        ans = kaijou(n)**2 * 2 % MOD
    else:
        n, m = max(n, m), min(n, m)
        ans = kaijou(n) * kaijou(m) % MOD
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
of = 10**9+7
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 % of)
else:
    print(math.factorial(n)*math.factorial(m) % of)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10**9 + 7
FAC = [1]
INV = [1]
for i in range(1,max(m,n)+1):
  FAC.append((FAC[i-1]*i) % MOD)
  INV.append(pow(FAC[-1],MOD-2,MOD))

def nPr(n,r):
  return FAC[n]*INV[n-r]

if abs(n-m) > 1:
  print(0)
else:
  ans = nPr(n,n)*nPr(m,m)
  ans %= MOD
  if n == m:
    ans *= 2
    ans %= MOD
  print(ans)

Submission status: Accepted

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

if abs(N-M) >= 2:
  print(0)
  exit()
  
N,M = max(N,M),min(N,M)
fctr_M = 1
for i in range(1,M+1):
  fctr_M *= i
  fctr_M %= MOD

ans = fctr_M * fctr_M
ans %= MOD
if N == M:
  ans *= 2
  ans %= MOD
else:
  ans *= N
  ans %= MOD

print(ans)

Submission status: Accepted

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

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

mod = 10**9+7
ans = 1
# 犬の組み合わせを取得
for i in range(1, n+1):
  ans *= i
  ans %= mod

# 犬の組み合わせに猿の組み合わせを入れる
for j in range(1, m+1):
  ans *= j
  ans %= mod

# 犬と猿が同数の場合は組み合わせが倍になる
# （例） 
# 同数の場合 ⚪⚫⚪⚫ or ⚫⚪⚫⚪
# 片方が多い場合 ⚪⚫⚪⚫⚪
if n == m:
  ans *= 2
  ans %= mod
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,m = map(int,ipt().split())
    mod = 10**9+7
    kj = [1,1]
    for i in range(2,max(m,n)+1):
        kj.append(kj[-1]*i%mod)
    if abs(n-m) >= 2:
        print(0)
    elif abs(n-m) == 1:
        print(kj[n]*kj[m]%mod)
    else:
        print((2*kj[n]**2)%mod)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N_inu, M_saru = map(int, input().split())

d = abs(N_inu - M_saru)

if (d > 1):
    ans = 0

else:
    if (d == 1):
        ans = math.factorial(N_inu) * math.factorial(M_saru)
    else:
        ans = 2 * math.factorial(N_inu) * math.factorial(M_saru)

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

print(ans)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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


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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
n, m = map(int, input().split())
if abs(n - m) > 1:
    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:
import math
N,M = map(int,input().split())
n = max(N,M)
m = min(N,M)
if abs(N-M) > 1:
  print(0)
  exit()
 
ncnt = math.factorial(n) % 1000000007
 
if abs(N-M) == 0:
  mcnt = 2 * ncnt
else:
  mcnt = math.factorial(m)
print(ncnt*mcnt % 1000000007)

Submission status: Accepted

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

ncnt = math.factorial(n) % 1000000007

if abs(N-M) == 0:
  mcnt = 2 * ncnt
else:
  mcnt = math.factorial(m)
print(ncnt*mcnt % 1000000007)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = map(int, readline().split())
    diff = abs(n - m)

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

        if diff == 0:
            ans *= 2
            ans %= MOD
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
n,m = iim()
mod = 1000_000_007

def prepare(n, MOD):
     # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs


fac,inv = prepare(max(n,m),mod)
if abs(n-m) == 1:
    print(fac[n]*fac[m]%mod)
elif abs(n-m) == 0:
    print(fac[n]*fac[m]*2%mod)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


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


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) == 0:
    for i in range(1, n+1):
        ans *= i
        ans %= MOD
    else:
        ans = 2 * pow(ans, 2, MOD) % MOD
elif abs(n-m) == 1:
    cnt_n = 1
    cnt_m = 1
    for i in range(1, n+1):
        cnt_n = cnt_n * i % MOD
    for i in range(1, m+1):
        cnt_m = cnt_m * i % MOD
    else:
        ans = cnt_n * cnt_m % MOD
else:
    ans = 0

print(ans)

Submission status: Accepted

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

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

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

if n == m:
   print(2*pow(ans, 2, mod)%mod)
else:
   print(pow(ans, 2, mod)*m%mod)

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10**9 + 7
ans = 1
for i in range(1, n+1):
    ans *= i
    ans %= mod

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

if n == m:
    ans *= 2
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n, m = map(int, input().split())

    #output
    #先頭が犬の場合
    #犬同士の隙間はn-1通りある。
    #ここのすべてに猿が入らないといけない
    #したがって、m = n-1でないといけない。
    #犬の並び方はn!通り
    #猿の並び方はm!通り

    #先頭が猿の場合
    #n = m-1でないといけない。
    mod = pow(10, 9) + 7
    dog = 1
    monkey = 1

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

    answer = 0
    # %%
    if m - n == 1:
        answer = dog * monkey % mod
    elif n- m == 1:
        answer = dog * monkey % mod
    elif n - m == 0:
        answer = dog * monkey * 2 % mod

    # %%
    print(answer)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

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

m = 1000000007

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

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

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

Submission status: Accepted

Solution code:
import math

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


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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math

def main():
    n, m = input_list()
    if abs(n-m) > 1:
        print(0)
        exit()
    mod = 10**9 + 7
    diff = abs(n-m)
    if diff == 1:
        ans = math.factorial(n) * math.factorial(m)
        print(ans % mod)
    else:
        ans = 2 * (math.factorial(n) * math.factorial(m))
        print(ans % mod)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

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

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

if N > M + 1:
  ans = 0
elif N == M + 1:
  ans = (factorial(N) * factorial(M)) % MOD
else:
  ans = (2 * factorial(N) ** 2) % MOD

print(ans)

Submission status: Accepted

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

    ans=(fact(n)*fact(m))%mod
    if n==m: print((2*ans)%mod)
    else: print(ans)

Submission status: Accepted

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

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

if abs(n-m)>1:
  ans = 0
else:
  ans = func(ans,n)
  ans = func(ans,m)
  if abs(n-m)==0:
    ans*=2
    ans%=div

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
def main():
    import math
    n, m = map(int, input().split())
    if n - m <= -2 or n - m >= 2:
        print(0)
        exit()

    else:
        if n != m:
            ans = math.factorial(n) % (10**9 + 7) * math.factorial(m) % (10**9 + 7)
        else:
            ans = math.factorial(n) * 2 % (10**9 + 7) * math.factorial(m) % (10**9 + 7)

    print(ans)


    

 
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mod = 10 ** 9 + 7
ans = 0
N1 = 1
M1 = 1
cnt = 1
while True:
    cnt += 1
    if cnt <= N:
        N1 *= cnt
    if cnt <= M:
        M1 *= cnt
    N1 %= mod
    M1 %= mod
    if (cnt > M) and (cnt > N):
        break

if N == M - 1:
    ans = N1 * M1

elif N - 1 == M:
    ans = N1 * M1

elif N == M:
    ans = N1 * M1 * 2

else:
    ans = 0
ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import deque
import pprint

# a^n
def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod
# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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)

if (n + m)%2 == 0:
    ans = ans*2%(10**9 + 7)

print(ans)

Submission status: Accepted

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

    for i in range(N):
        perm=(perm*(i+1))%((10**9)+7)
    ans=(perm*perm*2)%((10**9)+7)

elif abs(N-M)==1:
    for i in range(N):
        perm=(perm*(i+1))%((10**9)+7)
    for i in range(M):
        perm2=(perm2*(i+1))%((10**9)+7)
    
    ans=(perm*perm2)%((10**9)+7)

else:
    ans=0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial as fac
a, b = map(int, input().split())
if abs(a-b) > 1:
    print(0)
    exit()
af = fac(a)
bf = fac(b)
sosu = 10**9+7
if a==b:
    print((af*bf*2)%sosu)
else:
    print((af*bf)%sosu)

Submission status: Accepted

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

def kai(n):
    u = 1
    for i in range(1,n+1):
        u = (u*(i%(10**9+7)))%(10**9+7)
    return u
        
if abs(n-m)==1:
    print((kai(n)*kai(m))%(10**9+7))
elif n-m==0:
    print((kai(n)*kai(m)*2)%(10**9+7))
else:
    print(0)