#pragma region Region_1

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<ll, ll>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VP = vector<P>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
#define MOD 1000000007
const int INF = (int)1e9 + 10; // int max > 2*10^9
const long long INFL = (ll)2e18 + 10; // ll max > 9*10^18

template <class T, class C>
bool chmax(T& a, C b) { if (a < b) { a = b; return true; } return false; }
template <class T, class C>
bool chmin(T& a, C b) { if (a > b) { a = b; return true; } return false; }


template <class T> T sum(const vector<T>& v) {
    T res = 0;
    for (size_t i = 0; i < v.size(); ++i) res += v[i];
    return res;
}

/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { cout << endl; }

template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << " ";
    print(forward<Tail>(tail)...);
}

template <class T> void print(vector<T>& vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << " ";
    }
    cout << endl;
}

template <class T> void print(vector<vector<T>>& df) {
    for (auto& vec : df) {
        print(vec);
    }
}

#pragma endregion Region_1
/////////////////////////////////////////////////////////
struct mint {
    ll x;  // typedef long long ll;
    int mod;
    mint(ll x = 0, int mod = MOD) : x((x % mod + mod) % mod), mod(mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    // x^t
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;// 桁ずらし
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return (*this) *= a.inv(); }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

int main() {
    //入力の高速化用のコード
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    std::cout << std::setprecision(15);
    //////////////////////////////////////////
    ll n ,p;
    cin >> n >> p;

    mint x = p-2;
    x = x.pow(n-1) * (p-1);
    print(x.x);

    

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;

class FenwickTree {
private:
    int bit_size;
    vector<LL> bit;

public:
    FenwickTree(int siz): bit_size(siz), bit(siz + 1) {}

    void add(int num, LL val) {
        for(int i = num + 1; i <= bit_size; i += i & -i) {
            bit[i] += val;
        }
    }

    LL sum(int end) {
        LL res = 0;
        for(int i = end + 1; i > 0; i -= i & -i) {
            res += bit[i];
        }
        return res;
    }

    LL sum(int begin, int end) {
        return sum(end - 1) - sum(begin - 1);
    }

    void init(LL val) {
        for(int i = 1; i <= bit_size; i++) {
            add(i, val);
        }
    }

    int lower_bound(LL val) {
        if(val <= 0) return 0;
        int i = 0, n = 1;
        while(n * 2 <= bit_size) n *= 2;
        for(int k = n; k > 0; k /= 2) {
            if(i + k <= bit_size && bit[i + k] < val) {
                val -= bit[i + k];
                i += k;
            }
        }
        return i + 1;
    }
};

int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i,N) cin >> a[i];
  vector<LL> left(N), right(N), ans(N+1);
  LL res = 0;
  FenwickTree ftl(N), ftr(N);
  rep(i,N){
  	left[i] = 2 * ftl.sum(a[i]) - i;
  	res += i - ftl.sum(a[i]);
  	ftl.add(a[i], 1);
  }
  for(int i = N - 1; i >= 0; i--){
  	right[i] = 2 * ftr.sum(a[i]) - (N - 1 - i);
  	ftr.add(a[i], 1);
  }
  ans[N] = res;
  rep(i,N){
  	ans[N-1-i] = ans[N-i] + left[N-1-i] + right[N-1-i];
  }
  //cout << res << endl;
  rep(i,N) cout << ans[i] << endl;

  return 0;
}
