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:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



D = 10**9+7
def fact(n):
  ret = 1
  for i in range(1,n+1):
    ret = (ret*i) % D
  return ret


def main():
  NM = sorted(list(map(int, input().split())))
  n,m = [fact(NM[0]), fact(NM[1])]
  if NM[0]==NM[1]:
    return (n*m*2) % D
  elif NM[0]+1==NM[1]:
    return (n*m) % D
  else:
    return 0

if __name__ == "__main__":
  print(main())

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



D = 10**9+7

def main():
  NM = sorted(list(map(int, input().split())))
  if NM[1]-NM[0] > 1:
    return 0
  elif NM[0]==NM[1]:
    ret = 2*(math.factorial(NM[0])**2) % D
    return ret
  else:
    ret = math.factorial(NM[0])*math.factorial(NM[1]) % D
    return ret

if __name__ == "__main__":
  print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
a,b= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
if abs(a-b)>1:
    print(0)
    exit(0)
else :
    s = 1
    for i in range(1, a + 1):
        s*=i
        s%=rem
    for i in range(1, b + 1):
        s *= i
        s %= rem
    if a == b: s*=2
    s%=rem
    print(s)

Submission status: Accepted

Solution code:
from math import factorial
 

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

Submission status: Accepted

Solution code:
from functools import lru_cache


@lru_cache(maxsize=None)
def fact(n):
    ret = 1
    MOD = 10**9 + 7
    for i in range(1, n + 1):
        ret = ret * i % MOD
    return ret


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


if diff == 0:
    ans = (fact(N) * fact(M)) * 2
elif diff == 1:
    ans = fact(N) * fact(M)
else:
    ans = 0


ans %= MOD
print(ans)

Submission status: Accepted

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


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


if diff == 0:
    ans = (fact(N) * fact(M)) * 2
elif diff == 1:
    ans = fact(N) * fact(M)
else:
    ans = 0


ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import factorial


sys.setrecursionlimit(500000)
N, M = map(int, input().split())
MOD = 10**9 + 7
diff = abs(N - M)


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


ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
def mod_fact(n, mod):
    ans = 1
    for i in range(2, n + 1):
        ans = ans * i % mod
    return ans

mod = 10 ** 9 + 7

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

if N == M:
    print(2 * mod_fact(N, mod) ** 2 % mod)
elif abs(N - M) == 1:
    print(mod_fact(min(M, N), mod) ** 2 * max(M, N) % mod)
else:
    print(0)

Submission status: Accepted

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

mod = 10 ** 9 + 7

def mod_fact(n, mod = 10 ** 9 + 7):
    result = 1
    for i in range(2, n + 1):
        result = (result * i) % mod
    return result

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())
mod = int(1e9+7)

if N < M:
    N, M = M, N

if N == M:
    ans = (2 * (math.factorial(N)) ** 2) % mod

elif N == M + 1:
    ans = ((math.factorial(M)) ** 2 * N) % mod

else:
    ans = 0

print(ans)

Submission status: Accepted

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

for i in range(1, M+1) :
    monkey *= i
    monkey %= 10**9 + 7

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10**9+7
d = [1]*100001
for i in range(1,100001):
    d[i] = d[i-1] * i % mod

if N == M:
    a = d[N]
    print(2 * a * a % mod)
elif N == M + 1:
    a = d[N]
    b = d[M]
    print(a * b % mod)
elif N + 1 == M:
    a = d[N]
    b = d[M]
    print(a * b % mod)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
from sys import exit, stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
p_n = factorial(N)
p_m = factorial(M)
NUM = 10**9+7
if abs(N-M)==0:
    ans = 2*p_n*p_m
elif abs(N-M)==1:
    ans = p_n*p_m
else:
    ans = 0
print(ans%NUM)

Submission status: Accepted

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

MOD = 10 ** 9 + 7

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

print(ans)

Submission status: Accepted

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


nn = 1
for i in range(1, n + 1):
    nn *= i
    nn %= 10**9+7
mm = 1
for i in range(1, m + 1):
    mm *= i
    mm %= 10**9+7
if n==m:
    print(2*nn*mm%(10**9+7))
else:
    print(nn*mm%(10**9+7))

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce
# import statistics
import bisect
import copy
import fractions
import math
import pprint
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) 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 MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))
def unite(x, y):
    # それぞれのノードの根を求める
    x = root(x)
    y = root(y)
    if x == y:
        return
    # node[x]の根をyに変更する
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  # xが根の場合
        return x
    else:
        node[x] = root(node[x])  # 経路圧縮
        return node[x]
def dfs(v):
    # ノードに訪れた
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  # そもそもvとv2が辺でつながっていない
            continue
        if visited[v2] == True:  # もう見た
            continue
        dfs(v2)


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

    return n, m

def submit():
    n, m = read_input()
    limit = 10**9 + 7

    if n == m:
        acc = 1

        for i in range(1, n + 1):
            acc *= i
            if acc > limit:
                acc %= limit

        for i in range(1, m + 1):
            acc *= i
            if acc > limit:
                acc %= limit

        print(2*acc%limit)

    else:
        big = max(n, m)
        small = min(n, m)

        if big - small > 1:
            print(0)
        else:
            acc = 1
            for i in range(1, big + 1):
                acc *= i
                if acc > limit:
                    acc %= limit

            for i in range(1, small + 1):
                acc *= i
                if acc > limit:
                    acc %= limit

            print(acc)





if __name__ == '__main__':
    submit()

Submission status: Accepted

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

if abs(N-M) >= 2:
    print(0)
else:
    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:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

facterint = [1]
for i in range(1,max(N,M)+1):
    facterint.append(facterint[-1]*i%mod)


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

if N==M:
    ans = 2*facterint[N]*facterint[M]%mod
else:
    ans = facterint[N]*facterint[M]%mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
import math

divider = 1000000007

n, m = map(int, input().split())
diff = abs(n - m)

if diff == 0:
    print(2 * math.factorial(n) * math.factorial(m) % divider)
elif diff == 1:
    print(math.factorial(n) * math.factorial(m) % divider)
else:
    print(0)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
mod=1000000007
ans=1
if abs(a-b)>1:
  print(0)
else:
  if abs(a-b)==0:
    ans=2
  while a>1:
    ans*=a
    ans%=mod
    a-=1
  while b>1:
    ans*=b
    ans%=mod
    b-=1
  print(ans)

Submission status: Accepted

Solution code:
import math
MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math
n,m=map(int,input().split())
M=max(n,m)
mm=min(n,m)
if M-mm>1:
    ans=0
else:
    if n==m:
        ans=2*math.factorial(n)**2
    else:
        ans=M*math.factorial(mm)**2
print(ans%(1000000007))

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N,M = (int(_) for _ in input().split())
    ans = 0
    if(abs(N-M)>1):
        print(0)
        exit(0)
    if(abs(N-M)==1):
        ans = math.factorial(N) * math.factorial(M) % (10**9+7)
    else:
        ans = (math.factorial(N) * math.factorial(M))*2 % (10**9+7)
    print(ans)
    
if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

num_b = max([n,m]) #多い方
num_l = min([n,m]) #少ない方
ans = 0
if num_b - num_l >= 2: #差が2以上あると、隣接なしでは並べられない
  ans = 0
elif num_b - num_l == 1:
  #差が1匹の場合
  #多い方が(num_b)!通り、少ない方が(num_l)!通りの並び方なので、その積をとる
  ans = math.factorial(num_b) * math.factorial(num_l) % (10 ** 9 + 7)
else:
  #差が0匹の場合
  #一方を先に並べる。(num_b)!通り。もう一方をまず間に入れる。(num_l - 1)匹選ぶ方法がnum_l通りで並べ方が(num_l - 1)!通り残りを左右のどちらに置くかで2通り
  ans = math.factorial(num_b) * num_l * math.factorial(num_l - 1) * 2 % (10 ** 9 + 7)
  
print(ans)

Submission status: Accepted

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

D = 10**9 + 7
if abs(n-m) == 0:
    res = 1
    for i in range(1,n+1):
        res = (res * i)%D
        res = (res * i)%D
    res = (res * 2)%D
    print(res)
elif abs(n-m) == 1:
    n,m = max(n,m),min(n,m)
    res = 1
    for i in range(1,n+1):
        res = (res * i)%D
    for i in range(1,m+1):
        res = (res * i)%D
    print(res)
else:
    print(0)

Submission status: Accepted

Solution code:
import math

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

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

print(ans % MOD)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-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 pf(s): return print(s, flush=True)
N,M=LI()
if abs(N-M) >= 2:
    print(0)
else:
    ans=math.factorial(N)*math.factorial(M)
    if abs(N-M) == 0:
        ans *= 2
    print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [1 for _ in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

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

if abs(n - m) >= 2:
    print(0)
else:
    nn = memo[n] % mod
    mm = memo[m] % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
n = sorted([int(item) for item in input().split()])

def nPr(n, r):
    val = 1
    for i in range(r):
        val = val * (n-i) % MOD 
    return val

if n[1] - n[0] >= 2:
    print(0)
    exit()

ans = nPr(n[0], n[0]) * nPr(n[1], n[1])
if n[1] == n[0]:
    ans *= 2
ans %= MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

Submission status: Accepted

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

print(count % MOD)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,m=map(int,input().split())
def fact(n):
    mod=10**9+7
    if n==0:
        return 1
    else:
        return (n*fact(n-1))%mod
mod=10**9+7
if n==m:
    print(2*fact(n)*fact(m)%mod)
elif n==m+1 or m==n+1:
        print(fact(n)*fact(m)%mod)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x = list(map(int, input().split()))
MOD = 10 ** 9 + 7
if abs(x[0] - x[1]) > 1:
    print(0)
else:
    ans = 1
    for i in range(max(x)):
        for j in range(2):
            ans *= max(1, x[j] - i)
            ans = ans % MOD
    if x[0] == x[1]:
        print(ans * 2 % MOD)
    else:
        print(ans)

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:
import sys
from math import factorial

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    MOD = 10**9 + 7

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


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

Submission status: Accepted

Solution code:
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = float("inf")
mod = 10**9+7
n,m = map(int,input().split())
def factorial(x):
    ans = 1
    for i in range(1,x+1):
        ans =ans*i%mod
    return ans


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

Submission status: Accepted

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

mod = 1000000007

def modfact(N):
  answer=1
  while N>=2:
    answer=(answer*N)%mod
    N-=1
  return answer

if (N-M)%2==0:
  print((2*modfact(N)**2)%mod)
else:
  print((modfact(min(N,M))**2*max(N,M))%mod)

Submission status: Accepted

Solution code:
import math
inf = 10 ** 9 + 7
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) % inf)
else:
    print(math.factorial(N) * math.factorial(M) * 2 % inf)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())
a = 1
if N == M:
    # for i in range(1, N + 1):
    #     a *= i
    #     if a >= 10 ** 9 + 7:
    #         a -= 10 ** 9 + 7
    #    a = cai(N)
    # print((a * a * 2) )
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * 2) % (10 ** 9 + 7))
elif N == M + 1 or N + 1 == M:
    M, N = max(N, M), min(N, M)
    # for i in range(1, m + 1):
    #    a *= i
    # print((a * a * max(N, M)) % (10 ** 9 + 7))
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * M) % (10 ** 9 + 7))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def Factorial(A):
    MOD = 10**9+7
    X = 1
    for num in range(1, A+1):
        X *= num
        X %= MOD
    return X

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

if n == m+1:
    ans = Factorial(n)*Factorial(m)
elif n == m:
    ans = Factorial(n)*Factorial(m)*2
elif n+1 == m:
    ans = Factorial(n)*Factorial(m)
else:
    ans = 0
print(ans%MOD)

Submission status: Accepted

Solution code:
def fact_mod(n: int, mod: int)->int:
    ret = 1
    while n > 0:
        ret = (ret * n) % mod
        n -= 1
    return ret


def reconciled(N: int, M: int)->int:
    MOD = 10**9 + 7
    if abs(N - M) > 1:
        return 0
    if abs(N - M) == 1:
        return (fact_mod(N, MOD) * fact_mod(M, MOD)) % MOD

    return (2 * fact_mod(N, MOD) * fact_mod(M, MOD)) % MOD


if __name__ == "__main__":
    N, M = map(int, input().split())
    ans = reconciled(N, M)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

elif a == 1:
    md = math.factorial(M)
    nd = md * N
    print((md * nd) % amari)
else:
    print(0)

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import math
if abs (N - M) > 1:
    print(0)
else:
    if (N + M) % 2 == 0:
        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:
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
n = 1
for i in range(1, N+1):
    n *= i
    n %= MOD

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

Submission status: Accepted

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

Submission status: Accepted

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