Language: C++

#include<iostream>
#include<vector>
using namespace std;
typedef long long li;
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

li fact(li a){
  // vector,repa 必須
  static vector<li> f(1,1);

  if(f.size()>a)return f[a];
  if(df)print(a,"!");
  int n=f.size();
  repa(i,n,a+1){
    f.push_back(f[i-1]*i%MOD);
    if(df)print(" =",f[i]);
  }
  return f[a];
}

int find(int a,vector<int>& uf){
  if(uf[a]<0) return a;
  return uf[a]=find(uf[a],uf);
}

int unite(int a,int b,vector<int>& uf){
  a=find(a,uf); b=find(b,uf);
  if(a==b)return 1; 
  if(uf[a]>uf[b]) swap(a,b);
  uf[a]+=uf[b];
  uf[b]=a;
  return 0;
}

int size(int a,vector<int>& uf){
  a=find(a,uf);
  return -uf[a];
}


int main(){
  int n; cin >>n;
  vector<int> uf(n,-1),p;
  int f=0;
  rep(i,n){
    int a; cin >>a; a--;
    if(a==-2){
      p.push_back(i);
      continue;
    }
    if(unite(a,i,uf)) f++;
  }
  if(df) print("uf",uf,"f",f);
  int ps=p.size(),li s=0;
  rep(i,ps){
    int sz=size(p[i],uf);
    s+=sz;
    p[i]=sz;
  }
  if(df)print("p",p);
  s=n-s;
  vector<vector<li>> sgm(ps,vector<li>(ps,0));
  vector<li> pow(ps+1,1);
  repa(i,1,ps+1) pow[i]=pow[i-1]*(n-1)%MOD;
  if(df) print("pow",pow);
  if(ps==0){
    print(n-f);
    return 0;
  }
  
  sgm[0][0]=p[0];
  repa(i,1,ps){
    sgm[i][0]=(sgm[i-1][0]+p[i])%MOD;
    repa(j,1,i+1){
      sgm[i][j]=(sgm[i-1][j]+sgm[i-1][j-1]*p[i]%MOD)%MOD;
    }
  }
  
  if(df) rep(i,ps) print("sgm",sgm[i]);
  li mn=0;
  rep(i,ps){
    li s=sgm[ps-1][i]*pow[ps-1-i]%MOD;
    (mn+=s*fact(i)%MOD)%=MOD;
    if(df) print("-",s*fact(i));
  }
  if(df)print("mn",mn);
  (s=n*pow[ps-1])%=MOD;
  (s*=ps)%=MOD;
  if(df) print("s",s);
  (s+=(n-ps-f)*pow[ps]%MOD)%=MOD;
  if(df)print("+",(n-ps-f)*pow[ps]);
  print((s+MOD-mn)%MOD);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:69:22: error: expected initializer before 's'
   69 |   int ps=p.size(),li s=0;
      |                      ^
a.cc:72:5: error: 's' was not declared in this scope; did you mean 'sz'?
   72 |     s+=sz;
      |     ^
      |     sz
a.cc:76:3: error: 's' was not declared in this scope; did you mean 'ps'?
   76 |   s=n-s;
      |   ^
      |   ps
a.cc: In instantiation of 'void print(const T&, const Ts& ...) [with T = std::vector<int>; Ts = {char [2], int}]':
a.cc:10:122:   required from 'void print(const T&, const Ts& ...) [with T = char [3]; Ts = {std::vector<int, std::allocator<int> >, char [2], int}]'
   10 | template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
      |                                                                                                                     ~~~~~^~~~~~~
a.cc:68:15:   required from here
   68 |   if(df) print("uf",uf,"f",f);
      |          ~~~~~^~~~~~~~~~~~~~~
a.cc:10:79: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'const std::vector<int>')
   10 | template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
      |                                                                          ~~~~~^~~~
In file included from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/ostream:116:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(__ostream_type& (*)(__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  116 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:116:36: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)' {aka 'std::basic_ostream<char>& (*)(std::basic_ostream<char>&)'}
  116 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:125:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(__ios_type& (*)(__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>; __ios_type = std::basic_ios<char>]'
  125 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:125:32: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)' {aka 'std::basic_ios<char>& (*)(std::basic_ios<char>&)'}
  125 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:135:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  135 |       operator<<(ios_base& (*__pf) (ios_base&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:135:30: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'std::ios_base& (*)(std::ios_base&)'
  135 |       operator<<(ios_base& (*__pf) (ios_base&))
      |                  ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:174:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  174 |       operator<<(long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:174:23: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'long int'
  174 |       operator<<(long __n)
      |                  ~~~~~^~~
/usr/include/c++/14/ostream:178:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  178 |       operator<<(unsigned long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:178:32: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'long unsigned int'
  178 |       operator<<(unsigned long __n)
      |                  ~~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:182:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  182 |       operator<<(bool __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:182:23: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'bool'
  182 |       operator<<(bool __n)
      |                  ~~~~~^~~
In file included from /usr/include/c++/14/ostream:1022:
/usr/include/c++/14/bits/ostream.tcc:96:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]'
   96 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/ostream.tcc:97:22: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'short int'
   97 |     operator<<(short __n)
      |                ~~~~~~^~~
/usr/include/c++/14/ostream:189:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  189 |       operator<<(unsigned short __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:189:33: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'short unsigned int'
  189 |       operator<<(unsigned short __n)
      |                  ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ostream.tcc:110:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]'
  110 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/ostream.tcc:111:20: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'int'
  111 |     operator<<(int __n)
      |                ~~~~^~~
/usr/include/c++/14/ostream:200:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  200 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:200:31: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'unsigned int'
  200 |       operator<<(unsigned int __n)
      |                  ~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:211:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  211 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:211:28: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'long long int'
  211 |       operator<<(long long __n)
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/ostream:215:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  215 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:215:37: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'long long unsigned int'
  215 |       operator<<(unsigned long long __n)
      |                  ~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:231:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  231 |       operator<<(double __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:231:25: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'double'
  231 |       operator<<(double __f)
      |                  ~~~~~~~^~~
/usr/include/c++/14/ostream:235:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  235 |       operator<<(float __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:235:24: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'float'
  235 |       operator<<(float __f)
      |                  ~~~~~~^~~
/usr/include/c++/14/ostream:243:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  243 |       operator<<(long double __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:243:30: note:   no known conversion for argument 1 from 'const std::vector<int>' to 'long double'
  243 |       operator<<(long double __f)
      |                  ~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:301:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(con