#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define ll long long
#define LCBorz ios_base::sync_with_stdio(false);cin.tie(0);
const int N=1000005;
const int mod=1e6+3;
vector<ll> fac(N),inv(N);
ll pw(ll a,int b){
ll ans=1;
while(b>0){
if(b&1)ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
int32_t main(){
LCBorz;
fac[0]=inv[0]=1;
for(int i=1;i<N;i++){
fac[i]=fac[i-1]*i%mod;
inv[i]=pw(fac[i],mod-2);
}
int n,m;cin>>n>>m;
int p=n-m+1;
vector<int> v(p);
for(int i=0;i<p;i++){
cin>>v[i];
if(v[i]>m){
cout<<0<<endl;
return 0;
}
}
vector<int> ans(n,-1),vis(n,0);
auto upd=[&](int i,int j){
if(ans[i]==-1){
ans[i]=j;
}
else if(ans[i]!=j){
cout<<0<<endl;
exit(0);
}
};
vector<int> adj[N];
auto dfs=[&](auto dfs1,int k,int pa,int val)->void {
vis[k]=1;
upd(k,val);
for(int j:adj[k]){
if(j==pa)continue;
dfs1(dfs1,j,k,val);
}
};
for(int i=0;i<p-1;i++){
if(abs(v[i]-v[i+1])>1){
cout<<0<<endl;
return 0;
}
if(v[i]==v[i+1]+1){
upd(i,1);
upd(i+m,0);
}
else if(v[i]==v[i+1]-1){
upd(i,0);
upd(i+m,1);
}
else{
adj[i].push_back(i+m);
adj[i+m].push_back(i);
}
}
for(int i=0;i<n;i++){
if(ans[i]!=-1&&vis[i]==0){
dfs(dfs,i,i,ans[i]);
}
}
int a=0,b=v[0];
for(int i=0;i<m;i++){
if(ans[i]==-1){
a++;
}
if(ans[i]==1){
b--;
}
}
if(b>a||b<0){
cout<<0<<endl;
return 0;
}
cout<<fac[a]*inv[b]%mod*inv[a-b]%mod<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
13 |
Correct |
106 ms |
39600 KB |
Output is correct |
14 |
Correct |
106 ms |
39516 KB |
Output is correct |
15 |
Correct |
106 ms |
39580 KB |
Output is correct |
16 |
Correct |
108 ms |
39584 KB |
Output is correct |
17 |
Correct |
107 ms |
39516 KB |
Output is correct |
18 |
Correct |
105 ms |
39580 KB |
Output is correct |
19 |
Correct |
107 ms |
39516 KB |
Output is correct |
20 |
Correct |
105 ms |
39584 KB |
Output is correct |
21 |
Correct |
106 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
13 |
Correct |
106 ms |
39600 KB |
Output is correct |
14 |
Correct |
106 ms |
39516 KB |
Output is correct |
15 |
Correct |
106 ms |
39580 KB |
Output is correct |
16 |
Correct |
108 ms |
39584 KB |
Output is correct |
17 |
Correct |
107 ms |
39516 KB |
Output is correct |
18 |
Correct |
105 ms |
39580 KB |
Output is correct |
19 |
Correct |
107 ms |
39516 KB |
Output is correct |
20 |
Correct |
105 ms |
39584 KB |
Output is correct |
21 |
Correct |
106 ms |
39768 KB |
Output is correct |
22 |
Correct |
105 ms |
39588 KB |
Output is correct |
23 |
Correct |
107 ms |
39580 KB |
Output is correct |
24 |
Correct |
107 ms |
39512 KB |
Output is correct |
25 |
Correct |
106 ms |
39600 KB |
Output is correct |
26 |
Correct |
105 ms |
39516 KB |
Output is correct |
27 |
Correct |
105 ms |
39516 KB |
Output is correct |
28 |
Correct |
106 ms |
39764 KB |
Output is correct |
29 |
Correct |
105 ms |
39584 KB |
Output is correct |
30 |
Correct |
105 ms |
39516 KB |
Output is correct |
31 |
Correct |
104 ms |
39600 KB |
Output is correct |
32 |
Correct |
105 ms |
39588 KB |
Output is correct |
33 |
Correct |
105 ms |
39580 KB |
Output is correct |
34 |
Correct |
108 ms |
39584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
13 |
Correct |
106 ms |
39600 KB |
Output is correct |
14 |
Correct |
106 ms |
39516 KB |
Output is correct |
15 |
Correct |
106 ms |
39580 KB |
Output is correct |
16 |
Correct |
108 ms |
39584 KB |
Output is correct |
17 |
Correct |
107 ms |
39516 KB |
Output is correct |
18 |
Correct |
105 ms |
39580 KB |
Output is correct |
19 |
Correct |
107 ms |
39516 KB |
Output is correct |
20 |
Correct |
105 ms |
39584 KB |
Output is correct |
21 |
Correct |
106 ms |
39768 KB |
Output is correct |
22 |
Correct |
105 ms |
39588 KB |
Output is correct |
23 |
Correct |
107 ms |
39580 KB |
Output is correct |
24 |
Correct |
107 ms |
39512 KB |
Output is correct |
25 |
Correct |
106 ms |
39600 KB |
Output is correct |
26 |
Correct |
105 ms |
39516 KB |
Output is correct |
27 |
Correct |
105 ms |
39516 KB |
Output is correct |
28 |
Correct |
106 ms |
39764 KB |
Output is correct |
29 |
Correct |
105 ms |
39584 KB |
Output is correct |
30 |
Correct |
105 ms |
39516 KB |
Output is correct |
31 |
Correct |
104 ms |
39600 KB |
Output is correct |
32 |
Correct |
105 ms |
39588 KB |
Output is correct |
33 |
Correct |
105 ms |
39580 KB |
Output is correct |
34 |
Correct |
108 ms |
39584 KB |
Output is correct |
35 |
Correct |
224 ms |
82588 KB |
Output is correct |
36 |
Correct |
363 ms |
82004 KB |
Output is correct |
37 |
Correct |
304 ms |
84996 KB |
Output is correct |
38 |
Correct |
241 ms |
85676 KB |
Output is correct |
39 |
Correct |
213 ms |
83924 KB |
Output is correct |
40 |
Correct |
225 ms |
84304 KB |
Output is correct |
41 |
Correct |
315 ms |
85332 KB |
Output is correct |
42 |
Correct |
239 ms |
85192 KB |
Output is correct |
43 |
Correct |
238 ms |
85416 KB |
Output is correct |
44 |
Correct |
210 ms |
83796 KB |
Output is correct |
45 |
Correct |
239 ms |
118684 KB |
Output is correct |
46 |
Correct |
216 ms |
83864 KB |
Output is correct |
47 |
Correct |
212 ms |
83868 KB |
Output is correct |
48 |
Correct |
219 ms |
84288 KB |
Output is correct |
49 |
Correct |
261 ms |
86096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
13 |
Correct |
106 ms |
39600 KB |
Output is correct |
14 |
Correct |
106 ms |
39516 KB |
Output is correct |
15 |
Correct |
106 ms |
39580 KB |
Output is correct |
16 |
Correct |
108 ms |
39584 KB |
Output is correct |
17 |
Correct |
107 ms |
39516 KB |
Output is correct |
18 |
Correct |
105 ms |
39580 KB |
Output is correct |
19 |
Correct |
107 ms |
39516 KB |
Output is correct |
20 |
Correct |
105 ms |
39584 KB |
Output is correct |
21 |
Correct |
106 ms |
39768 KB |
Output is correct |
22 |
Correct |
105 ms |
39588 KB |
Output is correct |
23 |
Correct |
107 ms |
39580 KB |
Output is correct |
24 |
Correct |
107 ms |
39512 KB |
Output is correct |
25 |
Correct |
106 ms |
39600 KB |
Output is correct |
26 |
Correct |
105 ms |
39516 KB |
Output is correct |
27 |
Correct |
105 ms |
39516 KB |
Output is correct |
28 |
Correct |
106 ms |
39764 KB |
Output is correct |
29 |
Correct |
105 ms |
39584 KB |
Output is correct |
30 |
Correct |
105 ms |
39516 KB |
Output is correct |
31 |
Correct |
104 ms |
39600 KB |
Output is correct |
32 |
Correct |
105 ms |
39588 KB |
Output is correct |
33 |
Correct |
105 ms |
39580 KB |
Output is correct |
34 |
Correct |
108 ms |
39584 KB |
Output is correct |
35 |
Correct |
224 ms |
82588 KB |
Output is correct |
36 |
Correct |
363 ms |
82004 KB |
Output is correct |
37 |
Correct |
304 ms |
84996 KB |
Output is correct |
38 |
Correct |
241 ms |
85676 KB |
Output is correct |
39 |
Correct |
213 ms |
83924 KB |
Output is correct |
40 |
Correct |
225 ms |
84304 KB |
Output is correct |
41 |
Correct |
315 ms |
85332 KB |
Output is correct |
42 |
Correct |
239 ms |
85192 KB |
Output is correct |
43 |
Correct |
238 ms |
85416 KB |
Output is correct |
44 |
Correct |
210 ms |
83796 KB |
Output is correct |
45 |
Correct |
239 ms |
118684 KB |
Output is correct |
46 |
Correct |
216 ms |
83864 KB |
Output is correct |
47 |
Correct |
212 ms |
83868 KB |
Output is correct |
48 |
Correct |
219 ms |
84288 KB |
Output is correct |
49 |
Correct |
261 ms |
86096 KB |
Output is correct |
50 |
Correct |
227 ms |
85332 KB |
Output is correct |
51 |
Correct |
239 ms |
85644 KB |
Output is correct |
52 |
Correct |
248 ms |
85272 KB |
Output is correct |
53 |
Correct |
335 ms |
83260 KB |
Output is correct |
54 |
Correct |
283 ms |
85208 KB |
Output is correct |
55 |
Correct |
227 ms |
85232 KB |
Output is correct |
56 |
Correct |
241 ms |
85152 KB |
Output is correct |
57 |
Correct |
306 ms |
83540 KB |
Output is correct |
58 |
Correct |
233 ms |
85720 KB |
Output is correct |
59 |
Correct |
270 ms |
84816 KB |
Output is correct |
60 |
Correct |
334 ms |
83616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
39580 KB |
Output is correct |
2 |
Correct |
106 ms |
39516 KB |
Output is correct |
3 |
Correct |
105 ms |
39584 KB |
Output is correct |
4 |
Correct |
105 ms |
39516 KB |
Output is correct |
5 |
Correct |
105 ms |
39576 KB |
Output is correct |
6 |
Correct |
110 ms |
39580 KB |
Output is correct |
7 |
Correct |
106 ms |
39764 KB |
Output is correct |
8 |
Correct |
106 ms |
39580 KB |
Output is correct |
9 |
Correct |
106 ms |
39512 KB |
Output is correct |
10 |
Correct |
105 ms |
39516 KB |
Output is correct |
11 |
Correct |
106 ms |
39760 KB |
Output is correct |
12 |
Correct |
105 ms |
39584 KB |
Output is correct |
13 |
Correct |
106 ms |
39600 KB |
Output is correct |
14 |
Correct |
106 ms |
39516 KB |
Output is correct |
15 |
Correct |
106 ms |
39580 KB |
Output is correct |
16 |
Correct |
108 ms |
39584 KB |
Output is correct |
17 |
Correct |
107 ms |
39516 KB |
Output is correct |
18 |
Correct |
105 ms |
39580 KB |
Output is correct |
19 |
Correct |
107 ms |
39516 KB |
Output is correct |
20 |
Correct |
105 ms |
39584 KB |
Output is correct |
21 |
Correct |
106 ms |
39768 KB |
Output is correct |
22 |
Correct |
105 ms |
39588 KB |
Output is correct |
23 |
Correct |
107 ms |
39580 KB |
Output is correct |
24 |
Correct |
107 ms |
39512 KB |
Output is correct |
25 |
Correct |
106 ms |
39600 KB |
Output is correct |
26 |
Correct |
105 ms |
39516 KB |
Output is correct |
27 |
Correct |
105 ms |
39516 KB |
Output is correct |
28 |
Correct |
106 ms |
39764 KB |
Output is correct |
29 |
Correct |
105 ms |
39584 KB |
Output is correct |
30 |
Correct |
105 ms |
39516 KB |
Output is correct |
31 |
Correct |
104 ms |
39600 KB |
Output is correct |
32 |
Correct |
105 ms |
39588 KB |
Output is correct |
33 |
Correct |
105 ms |
39580 KB |
Output is correct |
34 |
Correct |
108 ms |
39584 KB |
Output is correct |
35 |
Correct |
224 ms |
82588 KB |
Output is correct |
36 |
Correct |
363 ms |
82004 KB |
Output is correct |
37 |
Correct |
304 ms |
84996 KB |
Output is correct |
38 |
Correct |
241 ms |
85676 KB |
Output is correct |
39 |
Correct |
213 ms |
83924 KB |
Output is correct |
40 |
Correct |
225 ms |
84304 KB |
Output is correct |
41 |
Correct |
315 ms |
85332 KB |
Output is correct |
42 |
Correct |
239 ms |
85192 KB |
Output is correct |
43 |
Correct |
238 ms |
85416 KB |
Output is correct |
44 |
Correct |
210 ms |
83796 KB |
Output is correct |
45 |
Correct |
239 ms |
118684 KB |
Output is correct |
46 |
Correct |
216 ms |
83864 KB |
Output is correct |
47 |
Correct |
212 ms |
83868 KB |
Output is correct |
48 |
Correct |
219 ms |
84288 KB |
Output is correct |
49 |
Correct |
261 ms |
86096 KB |
Output is correct |
50 |
Correct |
227 ms |
85332 KB |
Output is correct |
51 |
Correct |
239 ms |
85644 KB |
Output is correct |
52 |
Correct |
248 ms |
85272 KB |
Output is correct |
53 |
Correct |
335 ms |
83260 KB |
Output is correct |
54 |
Correct |
283 ms |
85208 KB |
Output is correct |
55 |
Correct |
227 ms |
85232 KB |
Output is correct |
56 |
Correct |
241 ms |
85152 KB |
Output is correct |
57 |
Correct |
306 ms |
83540 KB |
Output is correct |
58 |
Correct |
233 ms |
85720 KB |
Output is correct |
59 |
Correct |
270 ms |
84816 KB |
Output is correct |
60 |
Correct |
334 ms |
83616 KB |
Output is correct |
61 |
Correct |
109 ms |
47188 KB |
Output is correct |
62 |
Correct |
116 ms |
47416 KB |
Output is correct |
63 |
Correct |
113 ms |
47188 KB |
Output is correct |
64 |
Correct |
109 ms |
47184 KB |
Output is correct |
65 |
Correct |
179 ms |
81324 KB |
Output is correct |
66 |
Correct |
160 ms |
67480 KB |
Output is correct |
67 |
Correct |
241 ms |
80796 KB |
Output is correct |
68 |
Correct |
120 ms |
53664 KB |
Output is correct |
69 |
Correct |
109 ms |
47184 KB |
Output is correct |
70 |
Correct |
110 ms |
47264 KB |
Output is correct |
71 |
Correct |
109 ms |
47256 KB |
Output is correct |