Editorial for Stone Piles


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007

int solve(vector<int> a, int k)
{
    int n = a.size();
    int cnt = 0;
    if(a.front() < k-1 || a.back() < k-1) 
        return 0;
    for(int i = 0; i < n; ++i)
        if(a[i] > k)
            return 0;
    for(int i = 0; i < n-1; ++i)
        if(abs(a[i+1] - a[i]) > 1)
            return 0;
    int res = 1;
    for(int i = 0; i < n-1; ++i)
        if(a[i] == a[i+1] && a[i] != k)
            res = (res*2) % MOD;
    return res;
}

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    cout << solve(a, k) << endl;
    return 0;
}