#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(),(a).end() ll modpow(ll a, ll n, ll m) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % m; a = a * a % m; n >>= 1; } return res; } ll modinv(ll a, ll m) { return modpow(a, m - 2, m); } int main() { cin.tie(nullptr); ll P, N; cin >> P >> N; vector A(N); rep(i, 0, N) cin >> A[i]; string S; cin >> S; ll ans = A[0]; rep(i, 0, N - 1) { if (S[i] == '+') { ans = (ans + A[i + 1]) % P; } else if (S[i] == '-') { ans = (ans + 2 * P - A[i + 1]) % P; } else if (S[i] == '*') { ans = (ans * A[i + 1]) % P; } else { ans = (ans * modinv(A[i + 1], P)) % P; } } cout << ans << '\n'; }