This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll add(ll a,ll b,ll mod)
{
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b,ll mod)
{
    return ((a%mod)*(b%mod))%mod;
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,s,e;cin>>n>>s>>e;
    ll dp[n+5][n+5]={};
    dp[1][1]=1;
    for(ll i=2;i<=n;i++)
    {
        for(ll o=1;o<=i-1;o++)
        {
            if(i==s||i==e)
            {
                dp[i][o]=add(dp[i][o],dp[i-1][o],1000000007); // add to the first or last component
                dp[i][o+1]=add(dp[i][o],dp[i-1][o-1],1000000007); // create new component at the beginning or the end
            }
            else
            {
                dp[i][o+1]=add(dp[i][o+1],mul(dp[i-1][o],o+1-(i>s)-(i>e),1000000007),1000000007);
                if(o) dp[i][o-1]=add(dp[i][o-1],dp[i-1][o]*(o-1),1000000007);
            }
        }
    }
    cout<<dp[n][1];
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |