Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Lining Up

#include <bits/stdc++.h>

using namespace std;

const int mod = (int)1e9 + 7;

void mult(int &a, int b) {
    long long res = 1LL * a * b;
    a = res % mod;
}

int solve(vector<int> &a) {
    int n = a.size();

    map<int, int> slots;
    for (int i = 0; i < n; i++) {
        int diff = abs(i - (n - i - 1));
        slots[diff]++;
    }

    int ans = 1;
    for (int i = 0; i < n; i++) {
        if (slots[a[i]] == 0) {
            return 0;
        }
        mult(ans, slots[a[i]]);
        slots[a[i]]--;
    }

    for (auto &kv : slots) {
        if (kv.second != 0) {
            return 0;
        }
    }

    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    cout << solve(a) << endl;
    return 0;
}