luogu#P10216. 【模板】Pfaffian

【模板】Pfaffian

Background

A permutation π\pi of length 2n2n is called a perfect matching if and only if it satisfies:

  • 1in, π2i1<π2i\forall 1\le i\le n,\ \pi_{2i-1}<\pi_{2i}.
  • 1i<n, π2i1<π2i+1\forall 1\le i< n,\ \pi_{2i-1}<\pi_{2i+1}.

Let inv π\textup{inv }\pi denote the number of inversions of π\pi, and sgn π=(1)inv π\textup{sgn }\pi=(-1)^{\textup{inv }\pi}. Let M2n\mathfrak{M}_{2n} be the set of all perfect matchings of length 2n2n.

Let A=(ai,j)1i<j2n\mathbf{A}=(a_{i,j})_{1\le i<j\le 2n} be a skew-symmetric matrix. The Pfaffian\text{Pfaffian} of A\mathbf{A} is defined as

$$\textup{Pf}(\mathbf{A})=\sum\limits_{\pi\in\mathfrak{M}_{2n}}(\textup{sgn }\pi)\prod\limits_{i=1}^{n}a_{\pi(2i-1),\pi(2i)}$$

Problem Description

Given an even integer nn and a skew-symmetric matrix A=(ai,j)1i<jn\mathbf{A}=(a_{i,j})_{1\le i<j\le n}, find Pf(A)mod(109+7)\textup{Pf}(\mathbf{A}) \bmod (10^9+7).

Input Format

The first line contains a positive integer nn, and it is guaranteed that nn is even.

The next n1n-1 lines describe the matrix. On the ii-th line, there are nin-i non-negative integers, where the jj-th integer represents ai,i+ja_{i,i+j}.

Output Format

Output one non-negative integer, the answer.

4
1 2 3
4 5
6
8

Hint

For 30%30\% of the testdata, n10n\le 10.

For 100%100\% of the testdata, 2n5002\le n\le 500, 0ai,j<109+70\le a_{i,j}<10^9+7.

Translated by ChatGPT 5