思路:
首先,手动模拟样例可以知道,对于一个排列a(1~n按顺序排好),如果在第k个位置发生了一次交换,那么接下来两边都是独立的,我们可以想到用分治解决这个问题.于是我们假设f(l, r, st, ed)为原始排列(即1~n按顺序排号,为了方便,我们将所有的数+1)在区间[l, r]内,发生交换的数值的范围在[s, t]的方案总数,这里引入后两维状态是为了讨论在区间[l, r]内我们以哪个点为第1个交换的点,由于这个点可以将区间[l, r]分割成两个区间,然后分治到只有一个元素的时候,交换的数值只有一个,此时我们判断当前l 位置上,是否跟被交换的数值是一样的,如果是一样的这就属于一种合法的交换方案.由于区间左右两边的操作是独立的,所以要注意两件事.第一就是[l, r]区间的总方案数等于左边区间的总方案数乘以右区间的总方案数.第二就是,操作在时间上的先后顺序在两个区间是互相独立的,所以可以先操作左区间,也可以先操作右区间,因为对于[l, r]区间我们最开始已经交换了一次,用于划分两个区间,所以接下来只剩下r - l - 1次操作,在左右区间的乘积的基础上,我们还得再乘上C(r - l - 1, i - l),其中i是区间的分割点,i - l表示左区间的大小,当然用r - i - 1也可以,两个都是相等的.更加具体的实现细节可以看代码.
代码:
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 52, MOD = 1e9 + 7;
int n, p[N], f[N][N][N][N], vis[N][N][N][N], fact[N], infact[N], inv[N];
void init(int n) {
fact[0] = infact[0] = inv[0] = inv[1] = 1;
for (int i = 2; i <= n; ++i) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
for (int i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i % MOD;
infact[i] = infact[i - 1] * inv[i] % MOD;
}
}
int C(int n, int m) {
if (n < m) return 0;
if (m == 0 || n == m) return 1;
return fact[n] * infact[m] % MOD * infact[n - m] % MOD;
}
int dfs(int l, int r, int st, int ed) {
if (l == r) return st == p[l];
if (vis[l][r][st][ed]) return f[l][r][st][ed];
vis[l][r][st][ed] = 1;
int res = 0;
for (int i = l, x1, x2, y1, y2; i < r; ++i) {
if (l == r - 1) x1 = y1 = ed, x2 = y2 = st;
else if (i == l) x1 = y1 = i + 1, x2 = st, y2 = ed;
else if (i + 1 == r) x1 = st, y1 = ed, x2 = y2 = i;
else x1 = st, y1 = i + 1, x2 = i, y2 = ed;
res = (res + dfs(l, i, x1, y1) * dfs(i + 1, r, x2, y2) % MOD * C(r - l - 1, i - l) % MOD) % MOD;
}
return f[l][r][st][ed] = res;
}
signed main() {
qio >> n;
init(50);
for (int i = 1; i <= n; ++i) qio >> p[i], ++p[i];
qio << dfs(1, n, 1, n) << "\n";
}