#define PROBLEM "https://yukicoder.me/problems/no/1092" #pragma once //https://tatyam.hatenablog.com/entry/2019/12/15/003634 #include using namespace std; using ll=long long; template using pq=priority_queue,greater>; using pll=pair; const ll LINF=1LL<<60; #define _overload3(_1,_2,_3,name,...) name #define _overload4(_1,_2,_3,_4,name,...) name #define _rep1(i,n) for(ll i=0; i<(n); i++) #define _rep2(i,a,b) for(ll i=(a); i<(b); i++) #define _rep3(i,a,b,c) for(ll i=(a); i<(b); i+=(c)) #define rep(...) _overload4(__VA_ARGS__,_rep3,_rep2,_rep1)(__VA_ARGS__) #define _rrep1(i,n) for(ll i=(n); i-->0;) #define _rrep2(i,a,b) for(ll i=(b); i-->(a);) #define rrep(...) _overload3(__VA_ARGS__,_rrep2,_rrep1)(__VA_ARGS__) #define each(i,...) for(auto&& i:__VA_ARGS__) #define all(i) begin(i),end(i) #define rall(i) rbegin(i),rend(i) template bool chmin(T &a,const T &b){if(a>b){a=b;return true;}else return false;} template bool chmax(T &a,const T &b){if(a ll sum(const T &a){return accumulate(all(a),0LL);} template auto min(const T &a){return *min_element(all(a));} template auto max(const T &a){return *max_element(all(a));} inline int scan(){ return getchar(); } inline void scan(int &a){ scanf("%d", &a); } inline void scan(unsigned &a){ scanf("%u", &a); } inline void scan(long &a){ scanf("%ld", &a); } inline void scan(long long &a){ scanf("%lld", &a); } inline void scan(unsigned long long &a){ scanf("%llu", &a); } inline void scan(char &a){ cin >> a; } inline void scan(float &a){ scanf("%f", &a); } inline void scan(double &a){ scanf("%lf", &a); } inline void scan(long double &a){ scanf("%Lf", &a); } inline void scan(vector &vec){ for(unsigned i = 0; i < vec.size(); i++) { int a; scan(a); vec[i] = a; } } inline void scan(char a[]){ scanf("%s", a); } inline void scan(string &a){ cin >> a; } template inline void scan(vector &vec); template inline void scan(array &vec); template inline void scan(pair &p); template inline void scan(T (&vec)[size]); template inline void scan(vector &vec){ for(auto &i : vec) scan(i); } template inline void scan(deque &vec){ for(auto &i : vec) scan(i); } template inline void scan(array &vec){ for(auto &i : vec) scan(i); } template inline void scan(pair &p){ scan(p.first); scan(p.second); } template inline void scan(T (&vec)[size]){ for(auto &i : vec) scan(i); } template inline void scan(T &a){ cin >> a; } inline void in(){} template inline void in(Head &head, Tail&... tail){ scan(head); in(tail...); } inline void print(){ putchar(' '); } inline void print(const bool &a){ printf("%d", a); } inline void print(const int &a){ printf("%d", a); } inline void print(const unsigned &a){ printf("%u", a); } inline void print(const long &a){ printf("%ld", a); } inline void print(const long long &a){ printf("%lld", a); } inline void print(const unsigned long long &a){ printf("%llu", a); } inline void print(const char &a){ printf("%c", a); } inline void print(const char a[]){ printf("%s", a); } inline void print(const float &a){ printf("%.15f", a); } inline void print(const double &a){ printf("%.15f", a); } inline void print(const long double &a){ printf("%.15Lf", a); } inline void print(const string &a){ for(auto&& i : a) print(i); } template inline void print(const vector &vec); template inline void print(const array &vec); template inline void print(const pair &p); template inline void print(const T (&vec)[size]); template inline void print(const vector &vec){ if(vec.empty()) return; print(vec[0]); for(auto i = vec.begin(); ++i != vec.end(); ){ putchar(' '); print(*i); } } template inline void print(const deque &vec){ if(vec.empty()) return; print(vec[0]); for(auto i = vec.begin(); ++i != vec.end(); ){ putchar(' '); print(*i); } } template inline void print(const array &vec){ print(vec[0]); for(auto i = vec.begin(); ++i != vec.end(); ){ putchar(' '); print(*i); } } template inline void print(const pair &p){ print(p.first); putchar(' '); print(p.second); } template inline void print(const T (&vec)[size]){ print(vec[0]); for(auto i = vec; ++i != end(vec); ){ putchar(' '); print(*i); } } template inline void print(const T &a){ cout << a; } inline int out(){ putchar('\n'); return 0; } template inline int out(const T &t){ print(t); putchar('\n'); return 0; } template inline int out(const Head &head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } using ld=long double; using ull=unsigned long long; using uint=unsigned int; using pii=pair; using pdd=pair; using tuplis=array; #define vec(type,name,...) vector name(__VA_ARGS__); #define vv(type,name,h,...)vector> name(h,vector(__VA_ARGS__)); #define INT(...) int __VA_ARGS__; in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; in(__VA_ARGS__) #define ULL(...) ull __VA_ARGS__; in(__VA_ARGS__) #define STR(...) string __VA_ARGS__; in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; in(__VA_ARGS__) #define LD(...) ld __VA_ARGS__; in(__VA_ARGS__) #define VEC(type,name,size) vector name(size); in(name) #define VV(type, name, h, w) vector> name(h, vector(w)); in(name) template ld dsum(const T &a){return accumulate(all(a),0.0L);} const int INF=INT_MAX>>1; const ll MINF=1LL<<40; const ld DINF=numeric_limits::infinity(); const int MODD=1000000007; const int MOD=998244353; const ld EPS=1e-9; const ld PI=3.1415926535897932; const ll four[]={0,1,0,-1,0}; const ll eight[]={0,1,1,0,-1,-1,1,-1,0}; ll intpow(ll a,ll b){ll ret=1;while(b){if(b&1)ret*=a;a*=a;b>>=1;}return ret;} int Yes(bool i=true){return out(i?"Yes":"No");} int No(bool i=true){return out(i?"No":"Yes");} #define len(x) ((int)(x).size()) #define fi first #define se second #pragma once #include #include #include template struct MontgomeryModint{ static constexpr uint64_t _rev(){ uint64_t Nd=0; uint64_t t=0; uint64_t r=1ULL<<32; uint64_t i=1; while(r){ if(!(t&1)){ t+=N; Nd+=i; } t>>=1; r>>=1; i<<=1; } return Nd; } static constexpr uint32_t _phi(){ uint32_t prime_fact=N; uint32_t ret=N; for(int i=2; i*i>32; static constexpr uint64_t MASK=R-1; static constexpr uint32_t inv_power=_phi()-1; static_assert(1>32; if(ret>=N)return ret-N; return ret; } constexpr uint64_t val()noexcept{ return _reduction(x); } constexpr MontgomeryModint operator+()noexcept{return *this;} constexpr MontgomeryModint operator-()noexcept{return MontgomeryModint()-(*this);} constexpr friend MontgomeryModint operator+(MontgomeryModint lhs,MontgomeryModint rhs)noexcept{ return MontgomeryModint(lhs)+=rhs; } constexpr friend MontgomeryModint operator-(MontgomeryModint lhs,MontgomeryModint rhs)noexcept{ return MontgomeryModint(lhs)-=rhs; } constexpr friend MontgomeryModint operator*(MontgomeryModint lhs,MontgomeryModint rhs)noexcept{ return MontgomeryModint(lhs)*=rhs; } constexpr friend MontgomeryModint operator/(MontgomeryModint lhs,MontgomeryModint rhs){ return MontgomeryModint(lhs)/=rhs; } constexpr MontgomeryModint operator+=(MontgomeryModint rhs)noexcept{ x+=rhs.x; if(x>=N)x-=N; return *this; } constexpr MontgomeryModint operator-=(MontgomeryModint rhs)noexcept{ x-=rhs.x; if(x<0)x+=N; return *this; } constexpr MontgomeryModint operator*=(MontgomeryModint rhs)noexcept{ x=_reduction(x*rhs.x); return *this; } constexpr MontgomeryModint operator/=(MontgomeryModint rhs){ (*this)*=rhs.inv(); return *this; } constexpr MontgomeryModint& operator++(){ (*this)+=1; return *this; } constexpr MontgomeryModint& operator--(){ (*this)-=1; return *this; } constexpr MontgomeryModint operator++(int){ (*this)+=1; return *this; } constexpr MontgomeryModint operator--(int){ (*this)-=1; return *this; } constexpr bool operator==(MontgomeryModint rhs)noexcept{ return (x>=N?x-N:x)==(rhs.x>=N?rhs.x-N:rhs.x); } constexpr bool operator!=(MontgomeryModint rhs)noexcept{ return (x>=N?x-N:x)!=(rhs.x>=N?rhs.x-N:rhs.x); } constexpr MontgomeryModint inv(){ MontgomeryModint ret=(*this).pow(inv_power); assert(ret*(*this)==1); return ret; } constexpr MontgomeryModint pow(uint64_t x)noexcept{ MontgomeryModint ret=1; MontgomeryModint bin=(*this); while(x){ if(x&1)ret*=bin; bin*=bin; x>>=1; } return ret; } }; template struct DynamicMontgomeryModint{ static uint64_t _rev(uint32_t N){ uint64_t Nd=0; uint64_t t=0; uint64_t r=1ULL<<32; uint64_t i=1; while(r){ if(!(t&1)){ t+=N; Nd+=i; } t>>=1; r>>=1; i<<=1; } return Nd; } static uint32_t _phi(uint32_t N){ uint32_t prime_fact=N; uint32_t ret=N; for(int i=2; i*i>32; MASK=R-1; inv_power=_phi(N)-1; assert(1>32; if(ret>=N)return ret-N; return ret; } uint64_t val()noexcept{ return _reduction(x); } DynamicMontgomeryModint operator+()noexcept{return *this;} DynamicMontgomeryModint operator-()noexcept{return DynamicMontgomeryModint()-(*this);} friend DynamicMontgomeryModint operator+(DynamicMontgomeryModint lhs,DynamicMontgomeryModint rhs)noexcept{ return DynamicMontgomeryModint(lhs)+=rhs; } friend DynamicMontgomeryModint operator-(DynamicMontgomeryModint lhs,DynamicMontgomeryModint rhs)noexcept{ return DynamicMontgomeryModint(lhs)-=rhs; } friend DynamicMontgomeryModint operator*(DynamicMontgomeryModint lhs,DynamicMontgomeryModint rhs)noexcept{ return DynamicMontgomeryModint(lhs)*=rhs; } friend DynamicMontgomeryModint operator/(DynamicMontgomeryModint lhs,DynamicMontgomeryModint rhs){ return DynamicMontgomeryModint(lhs)/=rhs; } DynamicMontgomeryModint operator+=(DynamicMontgomeryModint rhs)noexcept{ x+=rhs.x; if(x>=N)x-=N; return *this; } DynamicMontgomeryModint operator-=(DynamicMontgomeryModint rhs)noexcept{ x-=rhs.x; if(x<0)x+=N; return *this; } DynamicMontgomeryModint operator*=(DynamicMontgomeryModint rhs)noexcept{ x=_reduction(x*rhs.x); return *this; } DynamicMontgomeryModint operator/=(DynamicMontgomeryModint rhs){ (*this)*=rhs.inv(); return *this; } DynamicMontgomeryModint& operator++(){ (*this)+=1; return *this; } DynamicMontgomeryModint& operator--(){ (*this)-=1; return *this; } DynamicMontgomeryModint operator++(int){ (*this)+=1; return *this; } DynamicMontgomeryModint operator--(int){ (*this)-=1; return *this; } bool operator==(DynamicMontgomeryModint rhs)noexcept{ return (x>=N?x-N:x)==(rhs.x>=N?rhs.x-N:rhs.x); } bool operator!=(DynamicMontgomeryModint rhs)noexcept{ return (x>=N?x-N:x)!=(rhs.x>=N?rhs.x-N:rhs.x); } DynamicMontgomeryModint inv(){ DynamicMontgomeryModint ret=(*this).pow(inv_power); assert(ret*(*this)==1); return ret; } DynamicMontgomeryModint pow(uint64_t x)noexcept{ DynamicMontgomeryModint ret=1; DynamicMontgomeryModint bin=(*this); while(x){ if(x&1)ret*=bin; bin*=bin; x>>=1; } return ret; } }; templateuint64_t DynamicMontgomeryModint::N; templateuint64_t DynamicMontgomeryModint::R; templateuint64_t DynamicMontgomeryModint::Nd; templateuint64_t DynamicMontgomeryModint::Rr; templateuint64_t DynamicMontgomeryModint::MASK; templateuint32_t DynamicMontgomeryModint::inv_power; using mint=DynamicMontgomeryModint<0>; int main(){ LL(P,n); VEC(ll,A,n); STR(S); if(P==2){ ll ans=A[0]; rep(i,n-1){ if(S[i]=='+'){ ans+=A[i+1]; } if(S[i]=='-'){ ans-=A[i+1]; } if(S[i]=='*'){ ans*=A[i+1]; } if(S[i]=='/'){ ans/=A[i+1]; } } return out(ans%2); } mint::set_mod(P); mint ans=A[0]; rep(i,n-1){ if(S[i]=='+'){ ans+=A[i+1]; } if(S[i]=='-'){ ans-=A[i+1]; } if(S[i]=='*'){ ans*=A[i+1]; } if(S[i]=='/'){ ans/=A[i+1]; } } out(ans.val()); }