#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
vector<ll> fact, inv_fact;
ll mod_pow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
ll mod_inv(ll a) {
return mod_pow(a, MOD - 2);
}
void precompute(int n) {
fact.resize(n + 1);
inv_fact.resize(n + 1);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i % MOD;
inv_fact[n] = mod_inv(fact[n]);
for (int i = n - 1; i >= 0; --i)
inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD;
}
ll C(int n, int k) {
return (k < 0 || k > n) ? 0 : fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
precompute(5000);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n);
vector<bool> present(n, false);
int total_missing = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] == -1) {
total_missing++;
} else {
present[a[i]] = true;
}
}
vector<int> missing;
for (int x = 0; x < n; ++x) {
if (!present[x]) missing.push_back(x);
}
ll total = 0;
for (int mex = 0; mex <= n; ++mex) {
// Проверка возможности MEX = mex
bool possible = true;
int required = 0; // Число обязательных пропусков для [0..mex-1]
for (int x = 0; x < mex; ++x) {
if (!present[x]) {
if (find(missing.begin(), missing.end(), x) == missing.end()) {
possible = false;
break;
}
required++;
}
}
if (!possible) continue;
// Проверка, что mex не присутствует в фиксированных элементах
if (mex < n && present[mex]) continue;
// Подсчёт вклада для текущего mex
for (int l = 0; l < n; ++l) {
int current_missing = 0;
int cnt_has = 0;
vector<bool> has(mex, false);
bool invalid = false;
for (int r = l; r < n; ++r) {
if (a[r] == -1) {
current_missing++;
} else {
if (a[r] == mex) {
invalid = true; // mex присутствует в подотрезке
} else if (a[r] < mex && !has[a[r]]) {
cnt_has++;
has[a[r]] = true;
}
}
if (invalid) break;
// Проверяем, все ли [0..mex-1] присутствуют или могут быть добавлены
int needed = required - (mex - cnt_has);
if (needed < 0) needed = 0;
if (current_missing >= needed) {
ll ways = C(total_missing - needed, current_missing - needed);
ways = ways * fact[required] % MOD; // Перестановки обязательных
ways = ways * fact[total_missing - required] % MOD; // Остальные
total = (total + ways * mex % MOD) % MOD;
}
}
}
}
cout << total << '\n';
}
return 0;
}