|
| 1 | +#include <cstdio> |
| 2 | +#include <cstring> |
| 3 | +#include <cstdlib> |
| 4 | +#include <cmath> |
| 5 | +#include <ctime> |
| 6 | +#include <cctype> |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <random> |
| 10 | +#include <bitset> |
| 11 | +#include <queue> |
| 12 | +#include <functional> |
| 13 | +#include <set> |
| 14 | +#include <map> |
| 15 | +#include <vector> |
| 16 | +#include <chrono> |
| 17 | +#include <iostream> |
| 18 | +#include <limits> |
| 19 | +#include <numeric> |
| 20 | + |
| 21 | +#define LOG(FMT...) fprintf(stderr, FMT) |
| 22 | + |
| 23 | +using namespace std; |
| 24 | + |
| 25 | +typedef long long ll; |
| 26 | +typedef unsigned long long ull; |
| 27 | + |
| 28 | +// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
| 29 | + |
| 30 | +template <class T> |
| 31 | +istream& operator>>(istream& is, vector<T>& v) { |
| 32 | + for (T& x : v) |
| 33 | + is >> x; |
| 34 | + return is; |
| 35 | +} |
| 36 | + |
| 37 | +template <class T> |
| 38 | +ostream& operator<<(ostream& os, const vector<T>& v) { |
| 39 | + if (!v.empty()) { |
| 40 | + os << v.front(); |
| 41 | + for (int i = 1; i < v.size(); ++i) |
| 42 | + os << ' ' << v[i]; |
| 43 | + } |
| 44 | + return os; |
| 45 | +} |
| 46 | + |
| 47 | +const int P = 1000000007; |
| 48 | + |
| 49 | +void add(int& x, int y) { |
| 50 | + if ((x += y) >= P) |
| 51 | + x -= P; |
| 52 | +} |
| 53 | + |
| 54 | +void sub(int& x, int y) { |
| 55 | + if ((x -= y) < 0) |
| 56 | + x += P; |
| 57 | +} |
| 58 | + |
| 59 | +int norm(int x) { return x >= P ? x - P : x; } |
| 60 | + |
| 61 | +int main() { |
| 62 | +#ifdef LBT |
| 63 | + freopen("test.in", "r", stdin); |
| 64 | + int nol_cl = clock(); |
| 65 | +#endif |
| 66 | + ios::sync_with_stdio(false); |
| 67 | + cin.tie(nullptr); |
| 68 | + |
| 69 | + int n; |
| 70 | + cin >> n; |
| 71 | + vector<int> a(n * 2), pos(n * 2, -1); |
| 72 | + cin >> a; |
| 73 | + for (int i = 0; i < n * 2; ++i) |
| 74 | + if (a[i] != -1) |
| 75 | + pos[--a[i]] = i; |
| 76 | + int empties = 0; |
| 77 | + for (int i = 0; i < n; ++i) |
| 78 | + if (a[i * 2] == -1 && a[i * 2 + 1] == -1) |
| 79 | + ++empties; |
| 80 | + vector<vector<int>> dp(n + 1, vector<int>(n + 1)); |
| 81 | + dp[0][0] = 1; |
| 82 | + for (int i = 0; i < n * 2; ++i) { |
| 83 | + if (pos[i] != -1 && a[pos[i] ^ 1] != -1) |
| 84 | + continue; |
| 85 | + vector<vector<int>> newDp(n + 1, vector<int>(n + 1)); |
| 86 | + if (pos[i] == -1) { |
| 87 | + for (int j = 0; j <= n; ++j) |
| 88 | + for (int k = 0; k <= n; ++k) { |
| 89 | + if (k < n) |
| 90 | + add(newDp[j][k + 1], dp[j][k]); |
| 91 | + if (j < n) |
| 92 | + add(newDp[j + 1][k], dp[j][k]); |
| 93 | + if (k) |
| 94 | + add(newDp[j][k - 1], dp[j][k]); |
| 95 | + } |
| 96 | + } else { |
| 97 | + for (int j = 0; j <= n; ++j) |
| 98 | + for (int k = 0; k <= n; ++k) { |
| 99 | + if (k < n) |
| 100 | + add(newDp[j][k + 1], dp[j][k]); |
| 101 | + if (j) |
| 102 | + add(newDp[j - 1][k], dp[j][k] * (ll)j % P); |
| 103 | + } |
| 104 | + } |
| 105 | + dp = newDp; |
| 106 | + } |
| 107 | + int ans = dp[0][0]; |
| 108 | + for (int i = 1; i <= empties; ++i) |
| 109 | + ans = ans * (ll)i % P; |
| 110 | + cout << ans << '\n'; |
| 111 | + |
| 112 | +#ifdef LBT |
| 113 | + LOG("Time: %dms\n", int ((clock() |
| 114 | + -nol_cl) / (double)CLOCKS_PER_SEC * 1000)); |
| 115 | +#endif |
| 116 | + return 0; |
| 117 | +} |
0 commit comments