#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 9, MOD = 998244353, LG = 11, BASE = 29;
long long fac[maxN][2], n;
long long fastPow(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % MOD;
    b >>= 1, a = a * a % MOD;
  }
  return ret;
}
long long ncr(long long n, long long k) {
  return fac[n][0] * fac[k][1] % MOD * fac[n - k][1] % MOD;
}
long long F(long long k) {
  return (MOD + fastPow((MOD + 1 - k) % MOD, n) - fastPow((MOD - k) % MOD, n)) %
         MOD;
}
long long calcRed() {
  long long ret = 0;
  long long pot = 1, negp = 1;
  for (int i = 0; i < n; i++) {
    long long cnd = ncr(n, i) * negp % MOD * F(pot) % MOD;
    pot = pot * 3 % MOD;
    negp = negp * (MOD - 1) % MOD;
    ret = (ret + cnd) % MOD;
  }
  return ret;
}
long long calcLones() {
  long long ret = 0;
  long long nonCol = (fastPow(3, n) + MOD - 3) % MOD;
  long long vacs = fastPow(nonCol, n - 1), invNonCol = fastPow(nonCol, MOD - 2);
  for (int i = 1; i <= n; i++) {
    long long cnd = vacs * ncr(n, i) % MOD;
    ret = (ret + cnd) % MOD;
    vacs = vacs * invNonCol % MOD;
  }
  return ret;
}
long long calcCombi() {
  long long ret = 0;
  long long nonCol = (fastPow(3, n) + MOD - 3) % MOD;
  long long vacs = fastPow(nonCol, n - 1), invNonCol = fastPow(nonCol, MOD - 2);
  long long bas = 3;
  for (int i = 1; i <= n; i++) {
    long long nice = (MOD + bas - 3) % MOD;
    long long niceWays = nice * ncr(n, i) % MOD;
    long long cnd = niceWays * vacs % MOD;
    ret = (ret + cnd) % MOD;
    bas = bas * 3 % MOD;
    vacs = vacs * invNonCol % MOD;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0][0] = fac[0][1] = 1;
  for (long long i = 1; i <= n; i++) fac[i][0] = fac[i - 1][0] * i % MOD;
  fac[n][1] = fastPow(fac[n][0], MOD - 2);
  for (long long i = n - 1; i > 0; i--)
    fac[i][1] = fac[i + 1][1] * (i + 1) % MOD;
  long long red = calcRed();
  long long lones = (MOD + calcLones() - red) % MOD;
  long long multi = calcCombi();
  long long ans = red * 3 + lones * 6 + multi * 2;
  cout << ans % MOD << '\n';
}
