#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) {
    if (k < 0 || k > n) return 0;
    return 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 k = 0; // Number of missing elements
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            if (a[i] == -1) {
                k++;
            } 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 each possible mex m
        for (int m = 0; m <= n; ++m) {
            if (m > 0 && !present[m-1] && find(missing.begin(), missing.end(), m-1) == missing.end()) {
                continue; // m-1 is missing and not in missing list (impossible)
            }

            bool mex_possible = true;
            for (int x = 0; x < m; ++x) {
                if (!present[x] && find(missing.begin(), missing.end(), x) == missing.end()) {
                    mex_possible = false;
                    break;
                }
            }
            if (!mex_possible) continue;

            vector<int> pos_m;
            if (m < n) {
                for (int i = 0; i < n; ++i) {
                    if (a[i] == m) pos_m.push_back(i);
                }
            }

            int cnt_m = pos_m.size();
            if (cnt_m > 0) continue; // m is present in fixed positions, cannot be mex

            // Calculate the number of intervals [l, r] that can have mex m
            // and the number of ways to fill the missing elements accordingly
            // This part requires efficient interval counting, which can be done in O(n^2)
            // Further optimization is needed here to reduce to O(n)
            // [This is a placeholder for the actual optimized logic]
        }

        cout << total << '\n';
    }

    return 0;
}