#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;
typedef long long ll;
#define all(V) V.begin(), V.end()

int mod = 998244353;

int nr[200001];

void add(int i, int y, int n) {
  while (i < n) {
    nr[i] += y;
    i += (i & ~(i - 1));
  }
}

int get(int i) {
  int ret = 0;
  while (i > 0) {
    ret += nr[i];
    i ^= (i & ~(i - 1));
  }
  return ret;
}

pair<ll, ll> egcd(ll a, ll b) {
  ll s = 0, os = 1;
  ll t = 1, ot = 0;
  ll r = b, orr = a;
  while (r) {
    ll c = orr / r;
    ll nr = orr - c * r;
    orr = r;
    r = nr;
    ll ns = os - c * s;
    os = s;
    s = ns;
    ll nt = ot - c * t;
    ot = t;
    t = nt;
  }
  if (a * (-os) + b * ot == 1) {
    os = -os;
  } else if (a * os + b * (-ot) == 1) {
    ot = -ot;
  } else if (a * (-os) + b * (-ot) == 1) {
    os = -os;
    ot = -ot;
  }
  return {os, ot};
}

int main() {
#ifdef DEBUG
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<int> P(n);
  for (auto &e: P) scanf("%d", &e);
  vector<int> wh(n + 1);
  for (auto &e: wh) e = -1;
  vector<int> in;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      wh[P[i]] = i;
      in.push_back(P[i]);
    }
  vector<int> oth;
  for (int i = 1; i <= n; i++)
    if (wh[i] == -1) oth.push_back(i);
  sort(all(in));
  ll ret = 0;
  auto j = 0;
  ll tba = 0, tbb = 0;
  vector<int> nr(n);
  for (int i = 0; i < n; i++)
    if (P[i] == -1) nr[i] = 1;
  for (int i = 1; i < n; i++)
    nr[i] += nr[i - 1];
  auto k = 0;
  for (k = 0; k < in.size(); k++) {
    auto o = in[k];
    auto p = wh[o];
    tbb += nr[n - 1] - nr[p];
    tbb %= mod;
  }
  int m = oth.size();
  ll mf = 1, mff = 1, mf2 = 1;
  for (int i = 1; i <= m; i++) {
    if (i != 2) mf2 *= i;
    mf *= i;
    mf %= mod;
  }
  for (int i = 1; i < m; i++) {
    mff *= i;
    mff %= mod;
  }
  for (auto e: oth) {
    while (j < in.size() && in[j] < e) {
      auto o = in[j];
      auto p = wh[o];
      tba += nr[p];
      tbb += mod - nr[n - 1] + nr[p];
      tba %= mod;
      tbb %= mod;
      j++;
    }

    ret += tba * mff;
    ret %= mod;
    ret += tbb * mff;
    ret %= mod;
  }
  ret %= mod;
  ll toadd = 1LL * m * (m - 1) / 2;
  toadd %= mod;
  toadd *= mf2;
  toadd %= mod;
  ret += toadd;
  ret %= mod;
  j = 0;
  ll cur = 0;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      cur += j - get(P[i]);
      cur %= mod;
      add(P[i], 1, n);
      j++;
    }
  ret += cur * mf;
  ret %= mod;
  auto e = egcd(mf, mod);
  ret *= (e.first + mod) % mod;
  ret %= mod;
  printf("%d\n", (int) ret);
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;
typedef long long ll;
#define all(V) V.begin(), V.end()

int mod = 998244353;

int nr[200001];

void add(int i, int y, int n) {
  while (i <= n) {
    nr[i] += y;
    i += (i & -i);
  }
}

int get(int i) {
  int ret = 0;
  while (i > 0) {
    ret += nr[i];
    i ^= (i & -i);
  }
  return ret;
}

pair<ll, ll> egcd(ll a, ll b) {
  ll s = 0, os = 1;
  ll t = 1, ot = 0;
  ll r = b, orr = a;
  while (r) {
    ll c = orr / r;
    ll nr = orr - c * r;
    orr = r;
    r = nr;
    ll ns = os - c * s;
    os = s;
    s = ns;
    ll nt = ot - c * t;
    ot = t;
    t = nt;
  }
  if (a * (-os) + b * ot == 1) {
    os = -os;
  } else if (a * os + b * (-ot) == 1) {
    ot = -ot;
  } else if (a * (-os) + b * (-ot) == 1) {
    os = -os;
    ot = -ot;
  }
  return {os, ot};
}

int main() {
#ifdef DEBUG
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<int> P(n);
  for (auto &e: P) scanf("%d", &e);
  vector<int> wh(n + 1);
  for (auto &e: wh) e = -1;
  vector<int> in;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      wh[P[i]] = i;
      in.push_back(P[i]);
    }
  vector<int> oth;
  for (int i = 1; i <= n; i++)
    if (wh[i] == -1) oth.push_back(i);
  sort(all(in));
  ll ret = 0;
  auto j = 0;
  ll tba = 0, tbb = 0;
  vector<int> nr(n);
  for (int i = 0; i < n; i++)
    if (P[i] == -1) nr[i] = 1;
  for (int i = 1; i < n; i++)
    nr[i] += nr[i - 1];
  auto k = 0;
  for (k = 0; k < in.size(); k++) {
    auto o = in[k];
    auto p = wh[o];
    tbb += nr[n - 1] - nr[p];
    tbb %= mod;
  }
  int m = oth.size();
  ll mf = 1, mff = 1, mf2 = 1;
  for (int i = 1; i <= m; i++) {
    if (i != 2) mf2 *= i;
    mf *= i;
    mf %= mod;
    mf2 %= mod;
  }
  for (int i = 1; i < m; i++) {
    mff *= i;
    mff %= mod;
  }
  for (auto e: oth) {
    while (j < in.size() && in[j] < e) {
      auto o = in[j];
      auto p = wh[o];
      tba += nr[p];
      tbb += mod - nr[n - 1] + nr[p];
      tba %= mod;
      tbb %= mod;
      j++;
    }

    ret += tba * mff;
    ret %= mod;
    ret += tbb * mff;
    ret %= mod;
  }
  ret %= mod;
  ll toadd = 1LL * m * (m - 1) / 2;
  toadd %= mod;
  toadd *= mf2;
  toadd %= mod;
  ret += toadd;
  ret %= mod;
  j = 0;
  ll cur = 0;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      cur += j - get(P[i]);
      cur %= mod;
      add(P[i], 1, n);
      j++;
    }
  ret += cur * mf;
  ret %= mod;
  auto e = egcd(mf, mod);
  ret *= (e.first + mod) % mod;
  ret %= mod;
  printf("%d\n", (int) ret);
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;
typedef long long ll;
#define all(V) V.begin(), V.end()

int mod = 998244353;

int nr[200001];

void add(int i, int y, int n) {
  while (i < n) {
    nr[i] += y;
    i += (i & ~(i - 1));
  }
}

int get(int i) {
  int ret = 0;
  while (i > 0) {
    ret += nr[i];
    i ^= (i & ~(i - 1));
  }
  return ret;
}

pair<ll, ll> egcd(ll a, ll b) {
  ll s = 0, os = 1;
  ll t = 1, ot = 0;
  ll r = b, orr = a;
  while (r) {
    ll c = orr / r;
    ll nr = orr - c * r;
    orr = r;
    r = nr;
    ll ns = os - c * s;
    os = s;
    s = ns;
    ll nt = ot - c * t;
    ot = t;
    t = nt;
  }
  if (a * (-os) + b * ot == 1) {
    os = -os;
  } else if (a * os + b * (-ot) == 1) {
    ot = -ot;
  } else if (a * (-os) + b * (-ot) == 1) {
    os = -os;
    ot = -ot;
  }
  return {os, ot};
}

int main() {
#ifdef DEBUG
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<int> P(n);
  for (auto &e: P) scanf("%d", &e);
  vector<int> wh(n + 1);
  for (auto &e: wh) e = -1;
  vector<int> in;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      wh[P[i]] = i;
      in.push_back(P[i]);
    }
  vector<int> oth;
  for (int i = 1; i <= n; i++)
    if (wh[i] == -1) oth.push_back(i);
  sort(all(in));
  ll ret = 0;
  auto j = 0;
  ll tb = 0;
  vector<int> nr(n);
  for (int i = 0; i < n; i++)
    if (P[i] == -1) nr[i] = 1;
  for (int i = 1; i < n; i++)
    nr[i] += nr[i - 1];
  ll cur = 1LL * in.size() * oth.size();
  for (auto e: oth) {
    while (j < in.size() && in[j] < e) {
      auto o = in[j];
      auto p = wh[o];
      tb += nr[n - 1] - nr[p];
      tb %= mod;
      j++;
    }
    cur -= tb;
    ret += cur;
    ret %= mod;
  }
  int m = oth.size();
  ret %= mod;
  ret += 1LL * m * (m - 1) / 2;
  ret %= mod;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      ret += get(P[i]);
      ret %= mod;
      add(P[i], 1, n);
    }
  ll mf = 1;
  for (int i = 1; i <= m; i++) {
    mf *= i;
    mf %= mod;
  }
  auto e = egcd(mf, mod);
  ret *= (e.first + mod) % mod;
  ret %= mod;
  printf("%d\n", (int) ret);
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;
typedef long long ll;
#define all(V) V.begin(), V.end()

int mod = 998244353;

int nr[200001];

void add(int i, int y, int n) {
  while (i <= n) {
    nr[i] += y;
    i += (i & -i);
  }
}

int get(int i) {
  int ret = 0;
  while (i > 0) {
    ret += nr[i];
    i ^= (i & -i);
  }
  return ret;
}

pair<ll, ll> egcd(ll a, ll b) {
  ll s = 0, os = 1;
  ll t = 1, ot = 0;
  ll r = b, orr = a;
  while (r) {
    ll c = orr / r;
    ll nr = orr - c * r;
    orr = r;
    r = nr;
    ll ns = os - c * s;
    os = s;
    s = ns;
    ll nt = ot - c * t;
    ot = t;
    t = nt;
  }
  if (a * (-os) + b * ot == 1) {
    os = -os;
  } else if (a * os + b * (-ot) == 1) {
    ot = -ot;
  } else if (a * (-os) + b * (-ot) == 1) {
    os = -os;
    ot = -ot;
  }
  return {os, ot};
}

int main() {
#ifdef DEBUG
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  int n;
  scanf("%d", &n);
  vector<int> P(n);
  for (auto &e: P) scanf("%d", &e);
  vector<int> wh(n + 1);
  for (auto &e: wh) e = -1;
  vector<int> in;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      wh[P[i]] = i;
      in.push_back(P[i]);
    }
  vector<int> oth;
  for (int i = 1; i <= n; i++)
    if (wh[i] == -1) oth.push_back(i);
  sort(all(in));
  ll ret = 0;
  auto j = 0;
  ll tba = 0, tbb = 0;
  vector<int> nr(n);
  for (int i = 0; i < n; i++)
    if (P[i] == -1) nr[i] = 1;
  for (int i = 1; i < n; i++)
    nr[i] += nr[i - 1];
  auto k = 0;
  for (k = 0; k < in.size(); k++) {
    auto o = in[k];
    auto p = wh[o];
    tbb += nr[n - 1] - nr[p];
    tbb %= mod;
  }
  int m = oth.size();
  ll mf = 1, mff = 1, mf2 = 1;
  for (int i = 1; i <= m; i++) {
    if (i != 2) mf2 *= i;
    mf *= i;
    mf %= mod;
  }
  for (int i = 1; i < m; i++) {
    mff *= i;
    mff %= mod;
  }
  for (auto e: oth) {
    while (j < in.size() && in[j] < e) {
      auto o = in[j];
      auto p = wh[o];
      tba += nr[p];
      tbb += mod - nr[n - 1] + nr[p];
      tba %= mod;
      tbb %= mod;
      j++;
    }

    ret += tba * mff;
    ret %= mod;
    ret += tbb * mff;
    ret %= mod;
  }
  ret %= mod;
  ll toadd = 1LL * m * (m - 1) / 2;
  toadd %= mod;
  toadd *= mf2;
  toadd %= mod;
  ret += toadd;
  ret %= mod;
  j = 0;
  ll cur = 0;
  for (int i = 0; i < n; i++)
    if (P[i] > 0) {
      cur += j - get(P[i]);
      cur %= mod;
      add(P[i], 1, n);
      j++;
    }
  ret += cur * mf;
  ret %= mod;
  auto e = egcd(mf, mod);
  ret *= (e.first + mod) % mod;
  ret %= mod;
  printf("%d\n", (int) ret);
  return 0;
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T>
struct fenwick_tree {
    int tree_n = 0;
    vector<T> tree;

    fenwick_tree(int n = 0) {
        if (n > 0)
            init(n);
    }

    void init(int n) {
        tree_n = n;
        tree.assign(tree_n + 1, 0);
    }

    // O(n) initialization of the Fenwick tree.
    void build(const auto &initial) {
        assert((int) initial.size() == tree_n);

        for (int i = 1; i <= tree_n; i++) {
            tree[i] = initial[i - 1];

            for (int k = (i & -i) >> 1; k > 0; k >>= 1)
                tree[i] += tree[i - k];
        }
    }

    // index is in [0, tree_n).
    void update(int index, const T &change) {
        assert(0 <= index && index < tree_n);

        for (int i = index + 1; i <= tree_n; i += i & -i)
            tree[i] += change;
    }

    // Returns the sum of the range [0, count).
    T query(int count) const {
        assert(count <= tree_n);
        T sum = 0;

        for (int i = count; i > 0; i -= i & -i)
            sum += tree[i];

        return sum;
    }

    // Returns the sum of the range [a, b).
    T query(int a, int b) const {
        return query(b) - query(a);
    }

    // Returns the element at index a in O(1) amortized across every index. Equivalent to query(a, a + 1).
    T get(int a) const {
        assert(0 <= a && a < tree_n);
        int above = a + 1;
        T sum = tree[above];
        above -= above & -above;

        while (a != above) {
            sum -= tree[a];
            a -= a & -a;
        }

        return sum;
    }

    // Returns the last i in [0, N] such that tree_query(i) <= sum, or -1 if no such i exists.
    int find_last_prefix(T sum) const {
        if (sum < 0)
            return -1;

        int prefix = 0;

        for (int k = 31 - __builtin_clz(tree_n); k >= 0; k--)
            if (prefix + (1 << k) <= tree_n && tree[prefix + (1 << k)] <= sum) {
                prefix += 1 << k;
                sum -= tree[prefix];
            }

        return prefix;
    }
};

const int MOD = 998244353;

long long mod_inv(long long a, long long m = MOD) {
    return a == 1 ? 1 : m - mod_inv(m % a, a) * m / a;
}

int N, M;
vector<int> P;
vector<int> missing_values;

int main() {
    scanf("%d", &N);
    P.resize(N);
    M = 0;
    vector<bool> seen(N + 1, false);

    for (int &p : P) {
        scanf("%d", &p);

        if (p < 0) {
            M++;
            p = 0;
        }

        seen[p] = true;
    }

    for (int i = 1; i <= N; i++)
        if (!seen[i])
            missing_values.push_back(i);

    assert((int) missing_values.size() == M);
    long long answer = (long long) M * (M - 1) % MOD * mod_inv(4) % MOD;

    fenwick_tree<int> tree(N + 1);
    long long inversions = 0;

    for (int i = N - 1; i >= 0; i--)
        if (P[i] > 0) {
            inversions += tree.query(P[i]);
            tree.update(P[i], +1);
        }

    answer = (answer + inversions) % MOD;
    long long sum = 0, running_count = 0;

    for (int i = 0; i < N; i++)
        if (P[i] > 0) {
            int location = lower_bound(missing_values.begin(), missing_values.end(), P[i]) - missing_values.begin();
            sum += running_count * (M - location) + (M - running_count) * location;
            sum %= MOD;
        } else {
            running_count++;
        }

    if (M > 0)
        sum = sum * mod_inv(M) % MOD;

    printf("%lld\n", (answer + sum) % MOD);
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

const int MOD = 998244353;

long long mod_inv(long long a, long long m = MOD) {
    return a == 1 ? 1 : m - mod_inv(m % a, a) * m / a;
}

int N, M;
vector<int> P;
vector<int> missing_values;

int main() {
    scanf("%d", &N);
    P.resize(N);
    M = 0;
    vector<bool> seen(N + 1, false);

    for (int &p : P) {
        scanf("%d", &p);

        if (p < 0) {
            M++;
            p = 0;
        }

        seen[p] = true;
    }

    for (int i = 1; i <= N; i++)
        if (!seen[i])
            missing_values.push_back(i);

    assert((int) missing_values.size() == M);
    long long answer = (long long) M * (M - 1) % MOD * mod_inv(4) % MOD;
    long long sum = 0, running_count = 0;

    for (int i = 0; i < N; i++)
        if (P[i] > 0) {
            int location = lower_bound(missing_values.begin(), missing_values.end(), P[i]) - missing_values.begin();
            sum += running_count * (M - location) + (M - running_count) * location;
            sum %= MOD;
        } else {
            running_count++;
        }

    if (M > 0)
        sum = sum * mod_inv(M) % MOD;

    printf("%lld\n", (answer + sum) % MOD);
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T>
struct fenwick_tree {
    int tree_n = 0;
    vector<T> tree;

    fenwick_tree(int n = 0) {
        if (n > 0)
            init(n);
    }

    void init(int n) {
        tree_n = n;
        tree.assign(tree_n + 1, 0);
    }

    // O(n) initialization of the Fenwick tree.
    void build(const auto &initial) {
        assert((int) initial.size() == tree_n);

        for (int i = 1; i <= tree_n; i++) {
            tree[i] = initial[i - 1];

            for (int k = (i & -i) >> 1; k > 0; k >>= 1)
                tree[i] += tree[i - k];
        }
    }

    // index is in [0, tree_n).
    void update(int index, const T &change) {
        assert(0 <= index && index < tree_n);

        for (int i = index + 1; i <= tree_n; i += i & -i)
            tree[i] += change;
    }

    // Returns the sum of the range [0, count).
    T query(int count) const {
        assert(count <= tree_n);
        T sum = 0;

        for (int i = count; i > 0; i -= i & -i)
            sum += tree[i];

        return sum;
    }

    // Returns the sum of the range [a, b).
    T query(int a, int b) const {
        return query(b) - query(a);
    }

    // Returns the element at index a in O(1) amortized across every index. Equivalent to query(a, a + 1).
    T get(int a) const {
        assert(0 <= a && a < tree_n);
        int above = a + 1;
        T sum = tree[above];
        above -= above & -above;

        while (a != above) {
            sum -= tree[a];
            a -= a & -a;
        }

        return sum;
    }

    // Returns the last i in [0, N] such that tree_query(i) <= sum, or -1 if no such i exists.
    int find_last_prefix(T sum) const {
        if (sum < 0)
            return -1;

        int prefix = 0;

        for (int k = 31 - __builtin_clz(tree_n); k >= 0; k--)
            if (prefix + (1 << k) <= tree_n && tree[prefix + (1 << k)] <= sum) {
                prefix += 1 << k;
                sum -= tree[prefix];
            }

        return prefix;
    }
};

const int MOD = 998244353;

int mod_add(int a, int b, int m = MOD) {
    int sum = a + b;
    return sum >= m ? sum - m : sum;
}

int mod_sub(int a, int b, int m = MOD) {
    int diff = a - b;
    return diff < 0 ? diff + m : diff;
}

int mod_mul(int a, int b, int m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
    return (uint64_t) a * b % m;
#endif
    // Mod multiplication optimized for Codeforces 32-bit machines.
    uint64_t x = (uint64_t) a * b;
    unsigned x_high = x >> 32, x_low = (unsigned) x;
    unsigned quot, rem;
    asm(
        "divl %4; \n\t"
        : "=a" (quot), "=d" (rem)
        : "d" (x_high), "a" (x_low), "r" (m)
    );
    return rem;
}

int mod_inv(int a, int m = MOD) {
    return a == 1 ? 1 : m - (uint64_t) mod_inv(m % a, a) * m / a;
}

int mod_pow(int a, long long p, int m = MOD) {
    assert(p >= 0);
    int result = 1;

    while (p > 0) {
        if (p & 1)
            result = mod_mul(result, a, m);

        a = mod_mul(a, a, m);
        p >>= 1;
    }

    return result;
}

struct mod_value {
    int value;

    mod_value() {
        value = 0;
    }

    mod_value(long long v, bool skip_checks = false) {
        if (!skip_checks) {
            if (v < 0)
                v = v % MOD + MOD;

            if (v >= MOD)
                v %= MOD;
        }

        value = v;
    }

    explicit operator int() const {
        return value;
    }

    mod_value& operator+=(const mod_value &other) {
        value = mod_add(value, other.value);
        return *this;
    }

    mod_value& operator-=(const mod_value &other) {
        value = mod_sub(value, other.value);
        return *this;
    }

    mod_value& operator*=(const mod_value &other) {
        value = mod_mul(value, other.value);
        return *this;
    }

    mod_value operator+(const mod_value &other) const {
        return mod_value(*this) += other;
    }

    mod_value operator-(const mod_value &other) const {
        return mod_value(*this) -= other;
    }

    mod_value operator*(const mod_value &other) const {
        return mod_value(*this) *= other;
    }

    mod_value& operator++() {
        value = value == MOD - 1 ? 0 : value + 1;
        return *this;
    }

    mod_value operator++(int) {
        mod_value before = *this;
        ++*this;
        return before;
    }

    mod_value& operator--() {
        value = value == 0 ? MOD - 1 : value - 1;
        return *this;
    }

    mod_value operator--(int) {
        mod_value before = *this;
        --*this;
        return before;
    }

    mod_value operator-() const {
        return mod_value(value == 0 ? 0 : MOD - value, true);
    }

    mod_value inv() const {
        return mod_value(mod_inv(value), true);
    }
};

ostream& operator<<(ostream& out, const mod_value &mv) {
    return out << (int) mv;
}

istream& operator>>(istream& in, mod_value &mv) {
    int x;
    in >> x;
    mv = x;
    return in;
}

int main() {
    int N;
    scanf("%d", &N);
    vector<int> P(N);
    vector<bool> seen(N + 1, false);
    int M = 0;

    for (int &p : P) {
        scanf("%d", &p);

        if (p < 0) {
            M++;
            p = 0;
        }

        seen[p] = true;
    }

    vector<int> missing_values;

    for (int i = 1; i <= N; i++)
        if (!seen[i])
            missing_values.push_back(i);

    assert((int) missing_values.size() == M);
    mod_value answer = mod_value(M) * mod_value(M - 1) * mod_value(4).inv();
    fenwick_tree<int> tree(N + 1);
    mod_value inversions = 0;

    for (int i = N - 1; i >= 0; i--)
        if (P[i] > 0) {
            inversions += tree.query(P[i]);
            tree.update(P[i], +1);
        }

    answer += inversions;
    mod_value sum = 0, running_count = 0;

    for (int i = 0; i < N; i++)
        if (P[i] > 0) {
            int location = lower_bound(missing_values.begin(), missing_values.end(), P[i]) - missing_values.begin();
            sum += running_count * (mod_value(M) - location) + (mod_value(M) - running_count) * location;
        } else {
            running_count++;
        }

    if (M > 0)
        sum *= mod_value(M).inv();

    answer += sum;
    cout << answer << '\n';
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T>
struct fenwick_tree {
    int tree_n = 0;
    vector<T> tree;

    fenwick_tree(int n = 0) {
        if (n > 0)
            init(n);
    }

    void init(int n) {
        tree_n = n;
        tree.assign(tree_n + 1, 0);
    }

    // O(n) initialization of the Fenwick tree.
    void build(const auto &initial) {
        assert((int) initial.size() == tree_n);

        for (int i = 1; i <= tree_n; i++) {
            tree[i] = initial[i - 1];

            for (int k = (i & -i) >> 1; k > 0; k >>= 1)
                tree[i] += tree[i - k];
        }
    }

    // index is in [0, tree_n).
    void update(int index, const T &change) {
        assert(0 <= index && index < tree_n);

        for (int i = index + 1; i <= tree_n; i += i & -i)
            tree[i] += change;
    }

    // Returns the sum of the range [0, count).
    T query(int count) const {
        assert(count <= tree_n);
        T sum = 0;

        for (int i = count; i > 0; i -= i & -i)
            sum += tree[i];

        return sum;
    }

    // Returns the sum of the range [a, b).
    T query(int a, int b) const {
        return query(b) - query(a);
    }

    // Returns the element at index a in O(1) amortized across every index. Equivalent to query(a, a + 1).
    T get(int a) const {
        assert(0 <= a && a < tree_n);
        int above = a + 1;
        T sum = tree[above];
        above -= above & -above;

        while (a != above) {
            sum -= tree[a];
            a -= a & -a;
        }

        return sum;
    }

    // Returns the last i in [0, N] such that tree_query(i) <= sum, or -1 if no such i exists.
    int find_last_prefix(T sum) const {
        if (sum < 0)
            return -1;

        int prefix = 0;

        for (int k = 31 - __builtin_clz(tree_n); k >= 0; k--)
            if (prefix + (1 << k) <= tree_n && tree[prefix + (1 << k)] <= sum) {
                prefix += 1 << k;
                sum -= tree[prefix];
            }

        return prefix;
    }
};

const int MOD = 998244353;

int mod_add(int a, int b, int m = MOD) {
    int sum = a + b;
    return sum >= m ? sum - m : sum;
}

int mod_sub(int a, int b, int m = MOD) {
    int diff = a - b;
    return diff < 0 ? diff + m : diff;
}

int mod_mul(int a, int b, int m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
    return (uint64_t) a * b % m;
#endif
    // Mod multiplication optimized for Codeforces 32-bit machines.
    uint64_t x = (uint64_t) a * b;
    unsigned x_high = x >> 32, x_low = (unsigned) x;
    unsigned quot, rem;
    asm(
        "divl %4; \n\t"
        : "=a" (quot), "=d" (rem)
        : "d" (x_high), "a" (x_low), "r" (m)
    );
    return rem;
}

int mod_inv(int a, int m = MOD) {
    return a == 1 ? 1 : m - (uint64_t) mod_inv(m % a, a) * m / a;
}

int mod_pow(int a, long long p, int m = MOD) {
    assert(p >= 0);
    int result = 1;

    while (p > 0) {
        if (p & 1)
            result = mod_mul(result, a, m);

        a = mod_mul(a, a, m);
        p >>= 1;
    }

    return result;
}

struct mod_value {
    int value;

    mod_value() {
        value = 0;
    }

    mod_value(long long v, bool skip_checks = false) {
        if (!skip_checks) {
            if (v < 0)
                v = v % MOD + MOD;

            if (v >= MOD)
                v %= MOD;
        }

        value = v;
    }

    explicit operator int() const {
        return value;
    }

    mod_value& operator+=(const mod_value &other) {
        value = mod_add(value, other.value);
        return *this;
    }

    mod_value& operator-=(const mod_value &other) {
        value = mod_sub(value, other.value);
        return *this;
    }

    mod_value& operator*=(const mod_value &other) {
        value = mod_mul(value, other.value);
        return *this;
    }

    mod_value operator+(const mod_value &other) const {
        return mod_value(*this) += other;
    }

    mod_value operator-(const mod_value &other) const {
        return mod_value(*this) -= other;
    }

    mod_value operator*(const mod_value &other) const {
        return mod_value(*this) *= other;
    }

    mod_value& operator++() {
        value = value == MOD - 1 ? 0 : value + 1;
        return *this;
    }

    mod_value operator++(int) {
        mod_value before = *this;
        ++*this;
        return before;
    }

    mod_value& operator--() {
        value = value == 0 ? MOD - 1 : value - 1;
        return *this;
    }

    mod_value operator--(int) {
        mod_value before = *this;
        --*this;
        return before;
    }

    mod_value operator-() const {
        return mod_value(value == 0 ? 0 : MOD - value, true);
    }

    mod_value inv() const {
        return mod_value(mod_inv(value), true);
    }

    bool operator<(const mod_value &other) const {
        return value < other.value;
    }

    bool operator>(const mod_value &other) const {
        return value > other.value;
    }

    bool operator<=(const mod_value &other) const {
        return value <= other.value;
    }

    bool operator>=(const mod_value &other) const {
        return value >= other.value;
    }

    bool operator==(const mod_value &other) const {
        return value == other.value;
    }

    bool operator!=(const mod_value &other) const {
        return value != other.value;
    }
};

ostream& operator<<(ostream& out, const mod_value &mv) {
    return out << (int) mv;
}

istream& operator>>(istream& in, mod_value &mv) {
    int x;
    in >> x;
    mv = x;
    return in;
}

int main() {
    int N;
    scanf("%d", &N);
    vector<int> P(N);
    vector<bool> seen(N + 1, false);
    mod_value M = 0;

    for (int &p : P) {
        scanf("%d", &p);

        if (p < 0) {
            M++;
            p = 0;
        }

        seen[p] = true;
    }

    vector<int> missing_values;

    for (int i = 1; i <= N; i++)
        if (!seen[i])
            missing_values.push_back(i);

    assert((int) missing_values.size() == (int) M);
    mod_value answer = mod_value(M) * mod_value(M - 1) * mod_value(4).inv();
    fenwick_tree<int> tree(N + 1);
    mod_value inversions = 0;

    for (int i = N - 1; i >= 0; i--)
        if (P[i] > 0) {
            inversions += tree.query(P[i]);
            tree.update(P[i], +1);
        }

    answer += inversions;
    mod_value sum = 0, running_count = 0;

    for (int i = 0; i < N; i++)
        if (P[i] > 0) {
            int location = lower_bound(missing_values.begin(), missing_values.end(), P[i]) - missing_values.begin();
            sum += running_count * (M - location) + (M - running_count) * location;
        } else {
            running_count++;
        }

    if (M > 0)
        sum *= mod_value(M).inv();

    answer += sum;
    cout << answer << '\n';
}


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);

	ans = ((cntn * (cntn - 1)) % MOD) * pw(4, MOD - 2);
	ans %= MOD;
	debug(ans);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	if(s.size() > 0) sort(all(s));
	//for(auto x : s) cerr << x << " ";
	ll lw, pr;
	for(int i = 0;i<n; i++){
		if(s.size() == 0) continue;
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);
			//debug(lw);
			if((ln[i] == cntn) or (ln[i] == 0)){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * ( ln[i] * pw( cntn, MOD - 2) );

			pr += (lw * ( (cntn - ln[i]) * pw(cntn, MOD - 2) ));

			pr %= MOD;
			//debug(pr);

			//debug(ans);
			pr %= MOD;
			ans += pr;
			//debug(ans);
			ans %= MOD;
			//debug(ans);
		}
	}

	cout << ans % MOD;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*
5
-1 -1 5 -1 1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);

	ans = ((cntn * (cntn - 1)) % MOD) * pw(4, MOD - 2);
	ans %= MOD;
	debug(ans);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	if(s.size() > 0) sort(all(s));
	//for(auto x : s) cerr << x << " ";
	ll lw, pr;
	for(int i = 0;i<n; i++){
		if(s.size() == 0) continue;
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);
			//debug(lw);
			if((ln[i] == cntn) or (ln[i] == 0)){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * (( ln[i] * pw( cntn, MOD - 2) ) % MOD);

			pr += (lw * ( ((cntn - ln[i]) * pw(cntn, MOD - 2) ) % MOD));

			pr %= MOD;
			//debug(pr);

			//debug(ans);
			pr %= MOD;
			ans += pr;
			//debug(ans);
			ans %= MOD;
			//debug(ans);
		}
	}

	cout << ans % MOD;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*
5
-1 -1 5 -1 1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);

	ans = (cntn * (cntn-1)) * pw(4, MOD - 2);
	ans %= MOD;

	//debug(ans);
	//while(a.size() < MAXN) a.pb(0);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	//debug("a");
	if(s.size() > 0) sort(all(s));
	//debug("b");
	ll lw, pr;
	for(int i = 0;i<n; i++){
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);

			if(ln[i] == cntn or ln[i] == 0){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * ( ln[i] * pw(cntn-ln[i],MOD - 2) );
			pr += (lw * ( (cntn-ln[i]) * pw(ln[i], MOD - 2) ));
			pr %= MOD;
			ans += pr;
			ans %= MOD;
		}
	}

	cout << ans % MOD;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*

                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);
	ll div = 4;
	ll d2 = 4;
	ans = (cntn * (cntn-1));
	ans %= MOD;

	//debug(ans);
	//while(a.size() < MAXN) a.pb(0);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += (cnt*div);
	debug(cnt);
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	//debug("a");
	if(s.size() > 0) sort(all(s));
	//debug("b");
	ll lw, pr;
	for(int i = 0;i<n; i++){
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);

			if(ln[i] == cntn or ln[i] == 0){
				if(ln[i] == cntn) ans += (div*(s.size() - lw));
				else ans += (lw*div);
				ans %= MOD;
				continue;
			}
			ll nd, na, nd2;
			nd = div * (cntn-ln[i]);
			nd2 = d2 * (cntn-ln[i]);
			na = (div * ((s.size() - lw) *ln[i])) + ((cntn-ln[i]) * ans);
			nd %= (MOD);
			nd2 %= (MOD- 1);
			na %= MOD;
			ans = na;
			div = nd;
			d2 = nd2;
			//pr = (s.size() - lw) * ( ln[i] * pw(cntn-ln[i],MOD - 2) );

			nd = div * ln[i];
			nd2 = d2 * ln[i];
			na = (div * ((lw) *(cntn-ln[i]))) + ((ln[i]) * ans);
			nd %= (MOD);
			nd2 %= (MOD-1);
			na %= MOD;
			ans = na;
			div = nd;
			d2 = nd2;
			//pr += lw * ( (cntn-ln[i]) * pw(ln[i], MOD - 2) );
			//pr %= MOD;
			//ans += pr;
			//ans %= MOD;
		}
	}
	ans %= MOD;
	div %= (MOD-1);
	debug(ans); debug(div);
	cout << (ans * pw(div, MOD - 2)) % MOD; ;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*
7
1 5 2 -1 -1 3 -1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);
	ll div = 4;
	ans = (cntn * (cntn-1));
	ans %= MOD;

	//debug(ans);
	//while(a.size() < MAXN) a.pb(0);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += (cnt*div);
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	//debug("a");
	if(s.size() > 0) sort(all(s));
	//debug("b");
	ll lw, pr;
	for(int i = 0;i<n; i++){
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);

			if(ln[i] == cntn or ln[i] == 0){
				if(ln[i] == cntn) ans += (div*(s.size() - lw));
				else ans += (lw*div);
				ans %= MOD;
				continue;
			}
			ll nd, na;
			nd = div * (cntn-ln[i]);
			na = (div * ((s.size() - lw) *ln[i])) + ((cntn-ln[i]) * ans);
			nd %= MOD;
			na %= MOD;
			ans = na;
			div = nd;

			//pr = (s.size() - lw) * ( ln[i] * pw(cntn-ln[i],MOD - 2) );

			nd = div * ln[i];
			na = (div * ((lw) *(cntn-ln[i]))) + ((ln[i]) * ans);
			nd %= MOD;
			na %= MOD;
			ans = na;
			div = nd;

			//pr += lw * ( (cntn-ln[i]) * pw(ln[i], MOD - 2) );
			//pr %= MOD;
			//ans += pr;
			//ans %= MOD;
		}
	}
	ans %= MOD;
	div %= MOD;
	cout << (ans * pw(div, MOD - 2)) % MOD; ;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*

                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);

	ans = (cntn * (cntn - 1)) * pw(4, MOD - 2);
	ans %= MOD;
	debug(ans);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	if(s.size() > 0) sort(all(s));
	//for(auto x : s) cerr << x << " ";
	ll lw, pr;
	for(int i = 0;i<n; i++){
		if(s.size() == 0) continue;
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);
			//debug(lw);
			if((ln[i] == cntn) or (ln[i] == 0)){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * ( ln[i] * pw( cntn, MOD - 2) );

			pr += (lw * ( (cntn - ln[i]) * pw(cntn, MOD - 2) ));

			pr %= MOD;
			//debug(pr);

			//debug(ans);
			pr %= MOD;
			ans += pr;
			//debug(ans);
			ans %= MOD;
			//debug(ans);
		}
	}

	cout << ans % MOD;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*
5
-1 -1 5 -1 1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);
	ll div = 4;
	ll d2 = 4;
	ans = (cntn * (cntn-1));
	ans %= MOD;

	//debug(ans);
	//while(a.size() < MAXN) a.pb(0);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += (cnt*div);
	debug(cnt);
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	//debug("a");
	if(s.size() > 0) sort(all(s));
	//debug("b");
	ll lw, pr;
	for(int i = 0;i<n; i++){
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);

			if(ln[i] == cntn or ln[i] == 0){
				if(ln[i] == cntn) ans += (div*(s.size() - lw));
				else ans += (lw*div);
				ans %= MOD;
				continue;
			}
			ll nd, na, nd2;
			nd = div * (cntn-ln[i]);
			nd2 = d2 * (cntn-ln[i]);
			na = (div * ((s.size() - lw) *ln[i])) + ((cntn-ln[i]) * ans);
			nd %= (MOD);
			nd2 %= (MOD- 1);
			na %= MOD;
			ans = na;
			div = nd;
			d2 = nd2;
			//pr = (s.size() - lw) * ( ln[i] * pw(cntn-ln[i],MOD - 2) );

			nd = div * ln[i];
			nd2 = d2 * ln[i];
			na = (div * ((lw) *(cntn-ln[i]))) + ((ln[i]) * ans);
			nd %= (MOD);
			nd2 %= (MOD-1);
			na %= MOD;
			ans = na;
			div = nd;
			d2 = nd2;
			//pr += lw * ( (cntn-ln[i]) * pw(ln[i], MOD - 2) );
			//pr %= MOD;
			//ans += pr;
			//ans %= MOD;
		}
	}
	ans %= MOD;
	div %= MOD;
	debug(ans); debug(div);
	cout << (ans * pw(d2, MOD - 2)) % MOD; ;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*
7
1 5 2 -1 -1 3 -1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}

	ans = ((cntn * (cntn - 1)) % MOD) * pw(4, MOD - 2);
	ans %= MOD;
	debug(ans);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	if(s.size() > 0) sort(all(s));
	ll lw, pr;
	for(int i = 0;i<n; i++){
		if(s.size() == 0) continue;
		if(p[i] != -1){
			lw = lower(p[i]);
			if((ln[i] == cntn) or (ln[i] == 0)){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * (( ln[i] * pw( cntn, MOD - 2) ) % MOD);

			pr += (lw * ( ((cntn - ln[i]) * pw(cntn, MOD - 2) ) % MOD));

			pr %= MOD;
			pr %= MOD;
			ans += pr;

			ans %= MOD;
		}
	}

	cout << ans % MOD;
	return 0;
}

/*
5
-1 -1 5 -1 1
                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 998244353ll;
const ll MAXN = (ll) 2e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;

ll pw(ll b, ll p){
	if(p == 0) return 1;
	b %= MOD;
	if(p % 2 == 1) return (b * pw(b, p - 1)) % MOD;
	return pw(b * b, p / 2);
}

vector<ll> a;

ll b[MAXN];
ll po, ii, jj, cnt = 0;
void merg(ll l, ll mid, ll r){
    po = 0;
    ii = l;
    jj = mid + 1;
    while((ii <= mid) and (jj <= r)){
        if(a[ii] > a[jj]){
            b[po] = a[jj];
            jj ++;
            cnt = cnt + (mid - ii + 1);
        } else {
            b[po] = a[ii];
            ii ++;
        }
        po++;
    }
    for(int i = ii; i <= mid; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = jj; i <= r; i++){
        b[po] = a[i];
        po++;
    }
    for(int i = 0;i < po;i++){
        a[l + i] = b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
}
void merge_sort(ll l, ll r){
    if(l >= r)return ;
    merge_sort(l,(l + r)/2);
    merge_sort((l + r)/2 + 1,r);
    merg(l,(l + r)/2,r);
}

ll p[MAXN];
ll ln[MAXN];
ll rn[MAXN];

ll vis[MAXN];
vector<ll> s;

ll lower(ll x){
	if(s.size() == 0) return 0;
	ll dw = -1;
	ll up = s.size();
	ll mid;
	while(dw + 1 < up){
		mid = (dw + up) / 2;
		if(s[mid] < x) dw = mid;
		else up = mid;
	}
	return dw + 1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	ll cntn = 0, ans = 0;
	for(int i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] == -1) cntn ++;
		else {
			a.pb(p[i]);
			vis[p[i]] = 1;
		}
		ln[i] = cntn;
	}
	//debug(cntn);

	ans = (cntn * (cntn-1)) * pw(4, MOD - 2);
	ans %= MOD;

	//debug(ans);
	//while(a.size() < MAXN) a.pb(0);
	if(a.size() == 0) a.pb(0);
	merge_sort(0, a.size() - 1);
	//debug(cnt);
	ans += cnt;
	ans %= MOD;

	for(int i = 1;i<=n;i++) if(vis[i] == 0) s.pb(i);
	//debug("a");
	if(s.size() > 0) sort(all(s));
	//debug("b");
	ll lw, pr;
	for(int i = 0;i<n; i++){
		////debug("i");
		if(p[i] != -1){

			lw = lower(p[i]);

			if(ln[i] == cntn or ln[i] == 0){
				if(ln[i] == cntn) ans += (s.size() - lw);
				else ans += lw;
				ans %= MOD;
				continue;
			}

			pr = (s.size() - lw) * ( ln[i] * pw(cntn-ln[i],MOD - 2) );
			pr += lw * ( (cntn-ln[i]) * pw(ln[i], MOD - 2) );
			pr %= MOD;
			ans += pr;
			ans %= MOD;
		}
	}

	cout << ans % MOD;
	//for(int i = 0;i<n;i++)
	return 0;
}

/*

                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/


import java.io.*;
import java.util.*;
import java.lang.*;

public class C implements Runnable
{
    @Override
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        int q = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        int[] arr = new int[q];
        for (int i = 0; i < q; i++) {
            arr[i] = in.nextInt();
        }

        w.println(print(arr));

        w.flush();
        w.close();
    }

    private static String print(int[] a) {
        Set<Integer> set = new HashSet<>();
        for (int num : a) {
            if (set.contains(num)) {
                set.remove(num);
            }
            else {
                set.add(num);
            }
        }
        int[] count = new int[32];
        for (int num : set) {
            int d = 0;
            int temp = num;
            while (temp > 0) {
                count[d++] += (temp & 1);
                temp = (temp >> 1);
            }
        }

        long max = 0;
        int first = -1;
        for (int num : set) {
            int[] c = new int[32];
            int temp = num;
            int d = 0;
            while (temp > 0) {
                c[d++] += (temp & 1);
                temp = (temp >> 1);
            }

            long val = 0;
            for (int i = 0; i < d; i++) {
                val = val * 2;
                if (count[i] == 1 && c[i] == 1) {
                    val += 1;
                }
            }

            if (val > max) {
                max = val;
                first = num;
            }
        }

        boolean add = false;
        StringBuilder sb = new StringBuilder();
        if (first == -1) {
            add = true;
        }
        else {
            sb.append(first);
        }

        for (int i = a.length - 1; i >= 0; i--) {
            if (a[i] == first && !add) {
                add = true;
                continue;
            }
            sb.append(" " + a[i]);
        }

        return sb.toString().trim();
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new C(),"Main",1<<27).start();
    }

}


import java.io.*;
import java.util.*;
import java.lang.*;

public class C implements Runnable
{
    @Override
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        int q = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        int[] arr = new int[q];
        for (int i = 0; i < q; i++) {
            arr[i] = in.nextInt();
        }

        w.println(print(arr));

        w.flush();
        w.close();
    }

    private static String print(int[] a) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : a) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        int first = -1;
        int l = 0;
        int[] count = new int[32];
        for (int num : a) {
            if (map.get(num) > 1) {
                continue;
            }
            int d = 0;
            int temp = num;
            while (temp > 0) {
                count[d++] += (temp & 1);
                temp = (temp >> 1);
            }
            if (d > l) {
                l = d;
                first = num;
            }
        }

        long max = 0;

        for (int num : a) {
            if (map.get(num) > 1) {
                continue;
            }
            int[] c = new int[32];
            int temp = num;
            int d = 0;
            while (temp > 0) {
                c[d++] += (temp & 1);
                temp = (temp >> 1);
            }
            if (d < l) {
                continue;
            }
            long val = 0;
            for (int i = 0; i < d; i++) {
                val = val * 2;
                if (count[i] == 1 && c[i] == 1) {
                    val += 1;
                }
            }
            System.out.println(num + " " + val);
            if (val > max) {
                max = val;
                first = num;
            }
        }

        boolean add = false;
        StringBuilder sb = new StringBuilder();
//        if (first == -1) {
//            add = true;
//        }
//        else {
            sb.append(first);
//        }
//
        for (int i = a.length - 1; i >= 0; i--) {
            if (a[i] == first && !add) {
                add = true;
                continue;
            }
            sb.append(" " + a[i]);
        }

        return sb.toString().trim();
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new C(),"Main",1<<27).start();
    }

}


import java.io.*;
import java.util.*;
import java.lang.*;

public class C implements Runnable
{
    @Override
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        int q = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        int[] arr = new int[q];
        for (int i = 0; i < q; i++) {
            arr[i] = in.nextInt();
        }

        w.println(print(arr));

        w.flush();
        w.close();
    }

    private static String print(int[] a) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : a) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        int[] count = new int[32];
        for (int num : a) {
            int d = 0;
            int temp = num;
            while (temp > 0) {
                count[d++] += (temp & 1);
                temp = (temp >> 1);
            }
        }

        long max = 0;
        int first = 0;
        for (int num : a) {
            int[] c = new int[32];
            int temp = num;
            int d = 0;
            while (temp > 0) {
                c[d++] += (temp & 1);
                temp = (temp >> 1);
            }

            long val = 0;
            for (int i = 0; i < d; i++) {
                val = val * 2;
                if (count[i] == map.get(num) && c[i] == 1) {
                    val += 1;
                }
            }

            if (val > max) {
                max = val;
                first = num;
            }
        }

        boolean add = false;
        StringBuilder sb = new StringBuilder();
        sb.append(first);
        for (int i = a.length - 1; i >= 0; i--) {
            if (a[i] == first && !add) {
                add = true;
                continue;
            }
            sb.append(" " + a[i]);
        }
        return sb.toString();
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new C(),"Main",1<<27).start();
    }

}


import java.io.*;
import java.util.*;
import java.lang.*;

public class C implements Runnable
{
    @Override
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        int q = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        int[] arr = new int[q];
        for (int i = 0; i < q; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);

        w.println(print(arr));

        w.flush();
        w.close();
    }

    private static String print(int[] a) {
        StringBuilder sb = new StringBuilder();
        for (int i = a.length - 1; i >= 0; i--) {
            sb.append(a[i] + " ");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new C(),"Main",1<<27).start();
    }

}


import java.io.*;
import java.util.*;
import java.lang.*;

public class C implements Runnable
{
    @Override
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        int q = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        int[] arr = new int[q];
        for (int i = 0; i < q; i++) {
            arr[i] = in.nextInt();
        }

        w.println(print(arr));

        w.flush();
        w.close();
    }

    private static String print(int[] a) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : a) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        int first = -1;
        int l = 0;
        int[] count = new int[32];
        for (int num : a) {
            if (map.get(num) > 1) {
                continue;
            }
            int d = 0;
            int temp = num;
            while (temp > 0) {
                count[d++] += (temp & 1);
                temp = (temp >> 1);
            }
            if (d >= l) {
                l = d;
                first = Math.max(first, num);
            }
        }

        long max = 0;

        for (int num : a) {
            if (map.get(num) > 1) {
                continue;
            }
            int[] c = new int[32];
            int temp = num;
            int d = 0;
            while (temp > 0) {
                c[d++] += (temp & 1);
                temp = (temp >> 1);
            }
            if (d < l) {
                continue;
            }
            long val = 0;
            for (int i = 0; i < d; i++) {
                val = val * 2;
                if (count[i] == 1 && c[i] == 1) {
                    val += 1;
                }
            }
            //System.out.println(num + " " + val);
            if (val > max) {
                max = val;
                first = num;
            }

        }

        boolean add = false;
        StringBuilder sb = new StringBuilder();
//        if (first == -1) {
//            add = true;
//        }
//        else {
            sb.append(first);
//        }
//
        for (int i = a.length - 1; i >= 0; i--) {
            if (a[i] == first && !add) {
                add = true;
                continue;
            }
            sb.append(" " + a[i]);
        }

        return sb.toString().trim();
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new C(),"Main",1<<27).start();
    }

}