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:
MOD = 10**9+7

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

def calc_total(n,m):
    if n == m:
        return (facto(n)*2)*facto(m)%MOD
    elif abs(n-m) == 1:
        return facto(n)*facto(m)%MOD
    elif abs(n-m) >= 2:
        return 0

n,m = map(int,input().split())
ans = calc_total(n,m)
print(ans)

Submission status: Accepted

Solution code:
import math

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

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)) % 1000000007)
else:
    print((math.factorial(N)*math.factorial(M)*2) % 1000000007)

Submission status: Accepted

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

diff = abs(N-M)
div = 10**9 + 7

if diff > 1:
    print(0)
elif diff == 1:
    ans = factorial(N) % div * factorial(M) % div % div
    print(ans)
else:
    ans = (((factorial(N) % div) ** 2) % div * 2) % div
    print(ans)

Submission status: Accepted

Solution code:
# encoding: utf-8
N, M = map(int, input().split())

diff = (N - M) if N > M else (M - N)

def bikkuri2(N, M):
    ans = 1
    for i in range(2, M + 1): ans = (ans * i) % (10 ** 9 + 7)
    ans0 = ans
    for i in range(M + 1, N + 1): ans = (ans * i) % (10 ** 9 + 7)
    ans1 = ans
    return (ans0, ans1)

N0, N1 = bikkuri2(N, M) if N > M else bikkuri2(M, N)
if diff < 1: print((2 * (N0 ** 2)) % (10 ** 9 + 7))
elif diff < 2: print((N0 * N1) % (10 ** 9 + 7))
else: print(0)

Submission status: Accepted

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

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

L = 0
if N == M:
    cnt = 1
    for i in range(1,N+1):
        cnt *= i
        cnt %= 1000000007
    ccnt = cnt
else:
    mi = min([N,M])
    ma = max([N,M])
    cnt = 1
    for i in range(1,mi+1):
        cnt *= i
        cnt %= 1000000007

    ccnt = cnt * ma


nn = N%2
mm = M%2
if (nn == 0 and mm == 0) or (nn == 1 and mm == 1):
    print((cnt * ccnt * 2) % 1000000007)
else:
    print((cnt * ccnt) % 1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ans = 0
if N == M:
    ans = 1
    for i in range(1, N+1):
        ans *= i**2
        ans %= 1000000007
    ans *= 2
    ans %= 1000000007

elif N+1 == M or N == M+1:
    ans = 1
    N = min(N, M)
    ans *= N+1
    for i in range(1, N+1):
        ans *= i**2
        ans %= 1000000007

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def 解():
    iN,iM = [int(_) for _ in input().split()]
    iD = 10**9+7

    if abs(iN - iM) <= 1:
        if iN < iM:
            iN,iM = iM,iN
        iFacM = 1
        for i in range(1,iM+1):
            iFacM *= i
            iFacM %=iD
        if iN == iM:
            print( iFacM ** 2 % iD * 2 % iD )
        else:
            #うーむ。たしかに。差分が1だからNかけるだけでいいわけだ。
            print( iFacM ** 2 % iD * iN % iD  )
    else:
        print(0)
解()

Submission status: Accepted

Solution code:
def 解():
    iN,iM = [int(_) for _ in input().split()]
    iD = 10**9+7

    if abs(iN - iM) <= 1:
        if iN < iM:
            iN,iM = iM,iN
        iFacM = 1
        for i in range(1,iM+1):
            iFacM *= i
            iFacM %=iD
        if iN == iM:
            print( iFacM * iFacM % iD * 2 % iD )
        else:
            iFacN = iFacM
            for i in range(iM+1,iN+1):
                iFacN *= i
                iFacN %=iD
            print( iFacM * iFacN % iD  )
    else:
        print(0)
解()

Submission status: Accepted

Solution code:
def 解():
    iN,iM = [int(_) for _ in input().split()]
    iD = 10**9+7

    if abs(iN - iM) <= 1:
        iFacM = 1
        for i in range(1,iM+1):
            iFacM *= i
            iFacM %=iD
        if iN == iM:
            print( iFacM * iFacM % iD * 2 % iD )
        else:
            iFacN = 1
            for i in range(1,iN+1):
                iFacN *= i
                iFacN %=iD
            print( iFacM * iFacN % iD  )
    else:
        print(0)
解()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mod = 1000000007
fact = [1]*150000
for i in range(1,150000):
  fact[i] = fact[i-1]*i % mod
if abs(n-m) > 1:
  print(0)
elif abs(n-m) == 1:
  print(fact[n]*fact[m]%mod)
else:
  print(fact[n]*fact[m]*2%mod)

Submission status: Accepted

Solution code:
from math import factorial
a,b = map(int,input().split())
if abs(a-b) > 1:
    print(0)
else:
    mul_ = 2-(abs(a-b))
    print(factorial(a) * factorial(b)*mul_%1000000007)

Submission status: Accepted

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

def kaijo(n):
    res = 1
    for i in range(n):
        res *= (n-i)
        res = res%(10**9+7)
    return res
    
if abs(N - M) >= 2:
    print(0)
elif N == M:
    print(((kaijo(N)*kaijo(M))*2)%(10**9+7))
else:
    print(((kaijo(N)*kaijo(M)))%(10**9+7))

Submission status: Accepted

Solution code:
from math import factorial as f

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

Submission status: Accepted

Solution code:
m=1000000007
f=lambda j,k:(j%m)*(k%m)%m
a,b=sorted(map(int,input().split()))
x=1
for i in range(2,a+1):
  x=f(x,i)
print(f(f(x,[x*b,x][a==b]),[1,2][a==b]) if b-a<2 else 0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

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

if abs(N - M) == 0:
    temp = math.factorial(N) * math.factorial(M) * 2 
    print(temp%div)
elif abs(N - M) == 1:
    temp = math.factorial(N) * math.factorial(M)
    print(temp % div)

else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mod = 10 ** 9 + 7
K = max(N, M)
kaijo = [1] * (K + 1) 

for i in range(K):
    kaijo[i + 1] = ((i + 1) * kaijo[i]) % mod
    
if abs(N - M) > 1:
    print(0)
    
else:
    res = 1
    res *= kaijo[N] * kaijo[M]
    if N == M:
        res *= 2
    
    print(res % mod)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mod = 10 ** 9 + 7
K = max(N, M)
kaijo = [1] * (K + 1) 

for i in range(K):
    kaijo[i + 1] = ((i + 1) * kaijo[i]) % mod
    
if abs(N - M) > 1:
    print(0)
    
else:
    res = 1
    res *= kaijo[N] * kaijo[M]
    if N == M:
        res *= 2
    
    print(res % mod)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
n,m = max(N,M), min(N,M)
MOD = int(1e9)+7

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
if n<m:
  n,m = m,n
if n == m:
  s = 1
  for i in range(1,m+1):
    s = s*i%1000000007
  s = s*s*2%1000000007
elif n == m+1:
  s = 1
  for i in range(1,m+1):
    s = s*i%1000000007
  s = s*s*n%1000000007
else:
  s = 0
print(s)

Submission status: Accepted

Solution code:
list1=[1]
for i in range(2,100001):
    list1.append((list1[len(list1)-1]*i)%(10**9+7))
N,M=map(int,input().split())
if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    print((list1[N-1]*list1[M-1])%(10**9+7))
elif N==M:
    print((list1[N-1]*list1[M-1]*2)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
mod = 1000000007
if abs(N - M) > 1:
    print(0)
else:
    fact = 1

    for i in range(1,max(N,M)+1):
        fact *= i
        fact %= mod

        if i == min(N,M):
            ans = fact

    ans *= fact
    ans %= mod

    if N == M:
        ans = 2*ans % mod
    else:
        ans = ans

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

Submission status: Accepted

Solution code:
def factorial(n):
    ans = 1
    for i in range(n,1,-1):
        ans*=i
        ans%=10**9+7
    return(ans)
        
N,M=list(map(int,input().split()))
ans=0
if N==M:
    ans=2*factorial(N)*factorial(M)%(10**9+7)
elif N+1==M or N==M+1:
    ans=factorial(N)*factorial(M)%(10**9+7)
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ans = 0
nn = 1
mm = 1
for i in range(1, N+1):
    nn = (nn*i)%(10**9+7)
for i in range(1, M+1):
    mm = (mm*i)%(10**9+7)

if N == M:
    ans = ((2*nn*mm)%(10**9+7))

elif abs(N-M) ==1:
    ans = ((nn*mm)%(10**9+7))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# nの階乗のリスト
def fact(n:int, mod:int) -> list:
    fac = [1,1]
    res = 1
    for i in range(2,n+1):
        res = res*i%mod
        fac.append(res)
    return fac

n,m = li()
MOD = 10**9 + 7

if m > n:
    n, m = m, n
    
fac = fact(n,MOD)

if n-m > 1:
    print(0)

elif n-m == 1:
    print((fac[n] * fac[m]) % MOD)
    
else:
    print((2 * fac[n] * fac[m]) % MOD)

Submission status: Accepted

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

MOD = 10**9+7

fac = [1]
for n in range(1, M + 2):
    fac.append((fac[-1] * n) % MOD)

if M == N:
    print((fac[M] * fac[N] * 2) % MOD)
else:
    print((fac[M] * fac[N]) % MOD)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
mod = 1000000000 + 7
def func(n):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= mod

    return r
N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    print(int((func(A)*func(A-1))%mod))
else:
    print(int(2*func(N)*func(N))%mod)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N, M = map(int, input().split())
    MOD = 10 ** 9 + 7
    if abs(N - M) >= 2:
        print(0)
        return

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

    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial

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


def f(n):
    if n == 1:
        return 1
    else:
        return n * f(n-1)


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

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

else:
    print((2 * factorial(n) ** 2) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
N,M=map(int, input().split())
mod=1000000000+7
fact=[1]*100001
for i in range(100000):
    fact[i+1]=fact[i]*(i+1)%mod

if N==M:
    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:
N, M = map(int, input().split())
MODN = 1000000000 + 7

fact = [1] * 100010
for i in range(len(fact)-1):
  fact[i+1] = fact[i] * (i+1) % MODN

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

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
N, M = min(N, M), max(N, M)

mod = 10 ** 9 + 7

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


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

print(ans)

Submission status: Accepted

Solution code:
Q = 10**9 + 7
'''
def factorial(n):
    if n == 0:
        return 1
    else:
        return (n * factorial(n-1))%Q
'''
N, M = map( int, input().split())
N, M = min(N, M), max(N, M)
if N == M or N+1 == M:
    NN = 1
    MM = 1
    for i in range(1,N+1):
        NN = (NN*i)%Q
    if N == M:
        ans = (2*NN*NN)%Q
    elif N+1 == M:
        ans = (NN*NN*M)%Q
else:
    ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def pi(n):
    v = 1
    for i in range(2, n + 1):
        v *= i
    return v


def main():
    N, M = li_input()

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

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


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10 ** 9 + 7
q = 100005
pow = [0] * q
pow[1] = 1
for i in range(2, q):
    pow[i] = pow[i - 1] * i
    pow[i] = pow[i] % mod

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

Submission status: Accepted

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

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

if n==m:
    print((2*factorial(n)**2)%mod)
elif n==m+1:
    print((n*factorial(m)**2)%mod)
elif n+1==m:
    print((m*factorial(n)**2)%mod)
else:
    print(0)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math

N,M = (int(i) for i in input().split())

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

Submission status: Accepted

Solution code:

mod = 10**9+7
n,m = map(int,raw_input().split())
def factorial(x):
	retval = 1
	for i in range(2,x+1):
		retval = (retval * i)% mod
	return retval

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

if max(n,m) > min(n,m):
	print (factorial(max(n,m)) * factorial(min(n,m))) % mod

else :
	print ((factorial(n)**2)%mod * 2)%mod

Submission status: Accepted

Solution code:
import math

n, m = [int(v) for v in input().split()]

mod = 10 ** 9 + 7
if abs(n - m) >= 2:
    print(0)
elif n == m:
    ans = math.factorial(n) % mod
    ans = ans * ans * 2 % mod
    print(ans)
elif abs(n - m) == 1:
    if n < m:
        n, m = m, n
    dog = math.factorial(n) % mod
    mon = math.factorial(m) % mod
    ans = dog * mon % mod
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import math

def fact(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
    return val

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:

def fact(n: int, mod: int) -> int:
    res = 1
    for i in range(n,1,-1):
        res = (res * i) % mod
        
    return res % mod

def judge(n:int, m:int, mod:int) -> int:
    if abs(n-m) >= 2:
        return 0
    elif abs(n-m) == 1:
        return (fact(n,mod) * fact(m,mod)) % mod
    else:
        return (2 * fact(n,mod) * fact(m,mod)) % mod
    
N, M = map(int, input().split())
MOD = 10**9+7

ans = judge(N,M,MOD)

print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

mod = 10 ** 9 + 7

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

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

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

if abs(N-M) > 1:
	print(0)
else:
	if N == M:
		ans = 1
		for i in range(2,N+1):
			ans *= i
			ans %= mod
		print((ans**2*2)%mod)
	else:
		MIN = min(N,M)
		ans1 = ans2 =1
		for i in range(2,MIN+1):
			ans1 *= i
			ans1 %= mod
		ans2 = ans1*max(N,M)%mod
		print(ans1*ans2%mod)

Submission status: Accepted

Solution code:
'''
https://beta.atcoder.jp/contests/abc065/submissions/1375280
'''

def f(x, d):
    ret = 1
    for i in range(1, x + 1):
        ret *= i
        if ret >= d:
            ret %= d
    return ret

nm = list(map(int, input().split()))
nm = sorted(nm)

div = (10 ** 9 + 7)

ans = 0
if nm[0] == nm[1]:
    ans = 2 * f(nm[0], div) ** 2 % div
elif nm[1] - nm[0] == 1:
    f0 = f(nm[0], div)
    ans = (f0 * ((nm[1] * f0) % div)) % div
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

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

# 2以上差があるときは、猿同士、犬同士が必ず隣り合うので0
if abs(N - M) > 1:
    re = 0
elif abs(N - M) == 1:
    re = (math.factorial(N) * math.factorial(M)) % (10**9 + 7)
else:
    re = (2 * math.factorial(N) * math.factorial(M)) % (10**9 + 7)

print(re)

Submission status: Accepted

Solution code:
from math import factorial

s=input().split()
N=int(s[0])
M=int(s[1])

if(M<N+2 and N<M+2):
    number=int(factorial(N)*factorial(M))

    if(N==M):number=number*2

    number = number % (10**9 +7)
    print(number)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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