結果

問題 No.696 square1001 and Permutation 5
ユーザー gew1fw
提出日時 2025-06-12 19:14:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 965 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 91,720 KB
最終ジャッジ日時 2025-06-12 19:14:46
合計ジャッジ時間 1,795 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

n = int(input())
p = list(map(int, input().split()))

# Precompute factorials modulo MOD
fact = [1] * (n + 1)
for i in range(1, n + 1):
    fact[i] = fact[i - 1] * i % MOD

class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 2)  # 1-based indexing

    def update(self, idx, delta):
        while idx <= self.n:
            self.tree[idx] += delta
            idx += idx & -idx

    def query(self, idx):
        res = 0
        while idx > 0:
            res += self.tree[idx]
            idx -= idx & -idx
        return res

# Initialize Fenwick Tree with all elements present
ft = FenwickTree(n)
for i in range(1, n + 1):
    ft.update(i, 1)

ans = 0
for i in range(n):
    current = p[i]
    # Number of elements less than current
    cnt = ft.query(current - 1)
    ans = (ans + cnt * fact[n - i - 1]) % MOD
    # Remove current element
    ft.update(current, -1)

print((ans + 1) % MOD)
0