CF1096F.Inversion Expectation

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A permutation of size nn is an array of size nn such that each integer from 11 to nn occurs exactly once in this array. An inversion in a permutation pp is a pair of indices (i,j)(i, j) such that i>ji > j and ai<aja_i < a_j . For example, a permutation [4,1,3,2][4, 1, 3, 2] contains 44 inversions: (2,1)(2, 1) , (3,1)(3, 1) , (4,1)(4, 1) , (4,3)(4, 3) .

You are given a permutation pp of size nn . However, the numbers on some positions are replaced by 1-1 . Let the valid permutation be such a replacement of 1-1 in this sequence back to numbers from 11 to nn in such a way that the resulting sequence is a permutation of size nn .

The given sequence was turned into a valid permutation randomly with the equal probability of getting each valid permutation.

Calculate the expected total number of inversions in the resulting valid permutation.

It can be shown that it is in the form of PQ\frac{P}{Q} where PP and QQ are non-negative integers and Q0Q \ne 0 . Report the value of PQ1(mod998244353)P \cdot Q^{-1} \pmod {998244353} .

输入格式

The first line contains a single integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — the length of the sequence.

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n ( 1pin-1 \le p_i \le n , pi0p_i \ne 0 ) — the initial sequence.

It is guaranteed that all elements not equal to 1-1 are pairwise distinct.

输出格式

Print a single integer — the expected total number of inversions in the resulting valid permutation.

It can be shown that it is in the form of PQ\frac{P}{Q} where PP and QQ are non-negative integers and Q0Q \ne 0 . Report the value of PQ1(mod998244353)P \cdot Q^{-1} \pmod {998244353} .

输入输出样例

  • 输入#1

    3
    3 -1 -1
    

    输出#1

    499122179
    
  • 输入#2

    2
    1 2
    

    输出#2

    0
    
  • 输入#3

    2
    -1 -1
    

    输出#3

    499122177
    

说明/提示

In the first example two resulting valid permutations are possible:

  • [3,1,2][3, 1, 2]22 inversions;
  • [3,2,1][3, 2, 1]33 inversions.

The expected value is 21+312=2.5\frac{2 \cdot 1 + 3 \cdot 1}{2} = 2.5 .

In the second example no 1-1 are present, thus the only valid permutation is possible — the given one. It has 00 inversions.

In the third example there are two resulting valid permutations — one with 00 inversions and one with 11 inversion.

首页