#include <bits/stdc++.h>
using namespace std;
long long n, F[1000005], invF[1000005], C[1000005], pow3[1000005];
long long mypow(long long a, long long b) {
  long long ans = 1;
  b %= 998244353;
  while (b > 0) {
    if (b & 1) ans = (ans * a) % 998244353;
    a = (a * a) % 998244353;
    b >>= 1;
  }
  return ans;
}
void precompute() {
  F[0] = pow3[0] = 1;
  for (long long i = 1; i <= n; i++) F[i] = (F[i - 1] * i) % 998244353;
  invF[n] = mypow(F[n], 998244353 - 2);
  for (long long i = n - 1; i >= 0; i--)
    invF[i] = (invF[i + 1] * (i + 1)) % 998244353;
  for (long long i = 0; i <= n; i++)
    C[i] = ((F[n] * invF[i]) % 998244353 * invF[n - i]) % 998244353;
  for (long long i = 1; i <= n; i++) pow3[i] = (pow3[i - 1] * 3) % 998244353;
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  precompute();
  long long ans0 = 0;
  for (long long i = 1; i <= n; i++) {
    long long temp = (pow3[i] * mypow(pow3[n], n - i)) % 998244353;
    temp = (temp * C[i]) % 998244353;
    if (i % 2 == 0) temp = -temp;
    ans0 = (ans0 + temp + 998244353) % 998244353;
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long temp =
        (mypow(1 - pow3[i], n) - mypow(-pow3[i], n) + 998244353) % 998244353;
    temp = (temp * C[i]) % 998244353;
    if (i % 2 == 0) temp = -temp;
    ans = (ans + temp + 998244353) % 998244353;
  }
  cout << (ans0 * 2 + ans * 3) % 998244353;
  return 0;
}
