#include <bits/stdc++.h>
#include <unordered_set>
#include <algorithm>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repll(i,n) for (ll i = 0; i < (ll)(n); i++)
#define fore(x,a) for(auto&(x) : (a))

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define ALL(a) (a).begin(), (a).end()

const ll INFL = 1e18;
const int INFI = 1e9;
const int MOD = 1e9 + 7;

int main(){
  ll N,P;
  cin >> N >> P;
  ll res = 1;
  res *= P - 1;
  ll i = P - 2;
  ll t = 0;
  ll s = 1;
  while(true) {
    if (t + s >= N) {
      break;
    }
    res *= i;
    res %= MOD;
    t += s;
    i = i*i%MOD;
    s *= 2;
  }
  for (int i = t + 1; i < N; i++) {
    res *= P - 2;
    res %= MOD;
  }
  cout << res << endl;
} 
#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

char rps(char l, char r) {
  if (l == r) return l;
  switch (l) {
    case 'R': return r == 'P' ? 'P' : 'R';
    case 'P': return r == 'S' ? 'S' : 'P';
    case 'S': return r == 'R' ? 'R' : 'S';
  }
  throw runtime_error("BAD");
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i32 n,k;
  cin>>n>>k;
  string s;
  cin>>s;
  rep(i,k) {
    string t = s;
    rep(j,n) {
      i32 l = (2*j)%n;
      i32 r = (2*j+1)%n;
      t[j] = rps(s[l], s[r]);
    }
    swap(s, t);
  }
  cout<<s[0]<<endl;
  return 0;
}
